Using custom placeholders in Bucket Search views

Sitecore lets you design custom views for searching item buckets – that is, when you’ve got so many items in a part of the tree that search is the only reliable option for finding the items.

However, while Sitecore comes with a number of ‘placeholders’ for the text to use in those views, it’s not obvious how to add more.

Well, I found this mentioned in this excellent post: Add Search Views to Sitecore 7, and here’s my take on it…

Sitecore lets you add new bucket views under /sitecore/system/Settings/Buckets/Views.

Careful choosing names for your view – I’ve blogged about some of the problems with this already and how they can break the default view.

Capture

Looking at the ‘Table’ view we can see it’s a lot like an ASP repeater; a header, footer, and repeating rows, which can use a number of placeholders (highlighted) to insert text. Sitecore comes with a number of these – but what if you want to add your own?

Well, you can. You can add a custom processor. As Keith Ball describes in his post:

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.

Right. What does that look like?

public class MyDynamicFieldsProcessor : DynamicFieldsProcessor
{
public override void Process(DynamicFieldsArgs args)
{
Item myItem = args.InnerItem.Database.GetItem(args.InnerItem.ID);
if (myItem != null)
{
// Do stuff with myItem to get values
args.QuickActions.Add("MyValue", "...");
args.QuickActions.Add("MyOtherValue", "...");
}
}
}

We define a processor class, and inherit from Sitecore.Buckets.Pipelines.UI.DynamicFields.DynamicFieldsProcessor. Then override process, and add key-value pairs for placeholder names and values to show in the view. Notice that you can add more than one value in the processor. Don’t use words like ‘Placeholder’ in the key name (see below).

Why we’re adding these to ‘QuickActions’ is a mystery to me, but there you go. Silly name.

Then patch this into your web.config:

<pipelines>
<buckets.dynamicFields>
<processor patch:after="processor[@type='Sitecore.Buckets.Pipelines.UI.DynamicFields.ItemTags, Sitecore.Buckets']"
type="MyAssembly.MyDynamicFieldsProcessor,MyAssembly" />
</buckets.dynamicFields>
</pipelines>

And now you can use these in your view – but with names in a give format. They’re the Key name you used, plus the text “DynamicPlaceholder“. Thus, in my example, it would be ‘MyValueDynamicPlaceholder‘ or ‘MyOtherValueDynamicPlaceholder‘.

And that’s it!

Do note that you can add multiple Key-Value pairs in one processor – you don’t need multiple ones.

Also note that this is processing before displaying the search results – it does not affect the search index itself, and so you can’t actually search on the values that you’re creating placeholders for (though that is possible to achieve separately; more on that in a future post.

Advertisements
Using custom placeholders in Bucket Search views

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s