Adding Search Views to Sitecore 7

Posted 02/05/2014 by Keith Ball

I want to go over a few nuances of the Search Bucket Views and show you how to add one using an example that was made for the First Sitecore Hackathon by Team Heisenberg.

First: The first two Views listed in the selection are not made like the last one (Using the three defaults)
Sitecore 7 Hardcoded Search Views 
The list of available views can be found under /Sitecore/System/Settings/Buckets/Views. It seems that the first two views are actually hard coded into the page. 
In “/Sitecore/shell/Applications/Buckets/Scripts/ItemBucket.utils.js” there is code that shows a TODO to update these two views.

function establishViews() {
    var a = $j("#ui_element");
    var defaultViews = ["list", "grid"];  //TODO: Hardcoded Logic
    var views; …

Second: Search Bucket Views work very much like ASP.NET Repeaters or ListViews. There is a Header, Footer, and Item Template.
 Sitecore 7 Search View Repeater Listview Likeness
In the instance of Repeaters, you can replace the typical <% Eval(‘ModelProperty’) %> with one of a set of pre-defined selection, or a custom selection. The Sitecore 7 Item Bucket PDF (5.6.7 Showing Dynamic Fields in Search Results) goes into more detail about adding new selections and the existing ones, but here is a cheat sheet.

Existing selections:

Placeholder  Description 
MetaPlaceholder The CSS style that you want to use when the results are displayed.
LaunchTypePlaceholder Whether it will launch the result in a new tab or in a new Content Editor window.
ItemIdPlaceholder The item ID.
ImagePathPlaceholder The path to the image of the item.
NamePlaceholder The name of the item
TemplatePlaceholder The name of the template that the item is based on.
BucketPlaceholder The bucket that this result comes from.
ContentPlaceholder The content of the result.
VersionPlaceholder The version of the content item.
CreatedPlaceholder The date that the content item was created.
CreatedByPlaceholder The person who created this item.

Making a custom selection can be done by appending to the bucket.dynamicFields pipeline.

<!-- Pipeline to add Workflow commands to Views -->
      <buckets.dynamicFields>
        <!-- Add the item workflow name to dynamic placeholders. KEY: Workflow -->         <processor patch:after="processor[@type='Sitecore.Buckets.Pipelines.UI.DynamicFields.ItemTags, Sitecore.Buckets']"                    type="Heisenberg.SCExtensions.Pipelines.BucketDynamicFields.GetWorkflowName,Heisenberg.SCExtensions" />
        <!-- Add the item workflow state name to dynamic placeholders. KEY: WorkflowState -->         <processor patch:after="processor[@type='Sitecore.Buckets.Pipelines.UI.DynamicFields.ItemTags, Sitecore.Buckets']"                    type="Heisenberg.SCExtensions.Pipelines.BucketDynamicFields.GetWorkflowStateName,Heisenberg.SCExtensions" />
        <!-- Add the item workflow history to dynamic placeholders. KEY: WorkflowHistoryActions -->         <processor patch:after="processor[@type='Sitecore.Buckets.Pipelines.UI.DynamicFields.ItemTags, Sitecore.Buckets']"                    type="Heisenberg.SCExtensions.Pipelines.BucketDynamicFields.GetWorkflowQuickActionHistory,Heisenberg.SCExtensions" />
        <!-- Add the item language name to the dynamic placeholders. KEY: Language -->         <processor patch:after="processor[@type='Sitecore.Buckets.Pipelines.UI.DynamicFields.ItemTags, Sitecore.Buckets']"                     type="Heisenberg.SCExtensions.Pipelines.BucketDynamicFields.GetItemLanguage,Heisenberg.SCExtensions" />
      </buckets.dynamicFields>
The processor must extend Sitecore.Buckets.Pipelines.UI.DynamicFields.DynamicFieldsProcessor and from there can append to the key value pair “QuickActions” that the arguments provide.
using Sitecore.Buckets.Pipelines.UI.DynamicFields;

namespace Heisenberg.SCExtensions.Pipelines.BucketDynamicFields
{
    public class GetItemLanguage : DynamicFieldsProcessor
    {
        /// <summary>
        ///     Add the item language to the dynamic placeholders for views
        /// </summary>
        /// <param name="args"></param>
        public override void Process(DynamicFieldsArgs args)
        {
            //Add the item language as a dynamic placeholder
            args.QuickActions.Add("Language", args.InnerItem.Language.Name);
        }
    }
}
You note above that my new dynamic field is called “Language” – When you want to use this dynamic field you will say “Language”DynamicPlaceholder (Without the quotes) to have it render whatever value you’ve set. This can be very simple like the one shown, or much more complex like returning a full block of HTML that can be controlled by jQuery in the footer (As can be seen in the second image.)

In the end, once you have all fields ready that you want to use you simply put that string in the item template field in Sitecore.
 Sitecore 7 Search View Use of Custom Fields

And finally! Once you have your view set up you simply check the "Enabled" checkbox in the View to add it to the list of views. It seems that "default" doesn't work yet. However, as was illustrated by the first part of this post, Sitecore still has plenty of work to put into making Views more modular and robust. The current design is still a great design to add new ones to your liking and expanding possible search result views.

If you would like the full source for this module or to see the other modules made by Team Heisenberg please check out our github.

Share:

Archive

Syndication