Dynamic Multilist with Search

Recently, in my project, I had a requirement where, I had to show Products in a Carousel. The Content Editor would choose some Products for the Carousel from a “MultiList with Search” field in the Carousel item. The Products are stored in a Bucket. The “MultiList with Search” is very useful in this case because the Products can be search in the control and also, in the Carousel template for the Source of the “MultiList with Search” field, we can give a StartSearchLocation and Filters like below, so that Sitecore will search from that location and apply the filters.

StartSearchLocation={92DC0456-1A2A-4AB5-A650-7053ACD119F5}&Filter=_name:*

The problem with my requirement was that, the Item Id or the path for the StartSearchLocation could be different for different website. The Products bucket for websites are located under the website node. So, I need a “Multilist with Search” , where the StartSearchLocation can change or in other words, I need to query to find the StartSearchLocation of the Products bucket for the website. Not too difficult to do this. Here is how I did it.

BucketList is the class I needed to extend. It is a FieldType and it is derived from SearchList. The Source property of the FieldTypes is what I had to change. This property is located in MultiListEx which is the parent class of SearchList. The StartSearchLocation now can contain query in addition to ItemId. If it contains the word ‘query:’ that means the StartSearchLocation ItemId has to be found from query. The following code is doing exactly that. If the word ‘query:’ is not present code assumes that StartSearchLocation contains the ItemId and just return the base.Source.

using Sitecore.Buckets.FieldTypes;
namespace Sitecore.SharedControls.Buckets.FieldTypes
{
    public class DynamicBucketList : BucketList
    {
        public new string Source
        {
            get { return base.Source; }
            set
            {
                if (value.Contains("query:"))
                {
                    var sourceString = value;
                    var queryString = sourceString.Substring(sourceString.IndexOf("query:"),
                        sourceString.Contains('&')
                            ? sourceString.IndexOf('&') - sourceString.IndexOf("query:")
                            : sourceString.Length - sourceString.IndexOf("query:"));
                    //Find out the item from the query. The query should return only one item.
                    var contextItem = global::Sitecore.Context.ContentDatabase.Items[this.ItemID];
                    var queryItem = contextItem.Axes.SelectSingleItem(queryString.Substring("query:".Length));
                    base.Source = value.Replace(queryString, queryItem.ID.ToString());
                }
                else
                {
                    base.Source = value;
                }
            }
        }
    }
}

After this I needed to create the new “Dynamic Multilist with Search” FieldType in core database like below.

Dynamic Multilist with Search

and added the following in a config file

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <controlSources>
      <source patch:after="*[@namespace='Sitecore.Buckets.FieldTypes']" mode="on" namespace="Sitecore.SharedControls.Buckets.FieldTypes" assembly="Sitecore.SharedControls" prefix="contentExtension" />
    </controlSources>
  </sitecore>
</configuration>

Selected the Field Type like below in the Carousel Template. The StartSearchLocation was a query.
StartSearchLocation=query:./ancestor-or-self::*[@@templatename=’Website’]/Product Repository/Products

Carousel

That’s it. Now, I can use a query to dynamically select the StartSearchLocation for the Multilist with Search.

Advertisements

About Himadri Chakrabarti

I am a software developer architect and a Sitecore MVP. My professional interest is everything and anything related to Software Architecture, .NET, Sitecore, Node.js, NoSQL etc. Outside of my profession, I am a hobbyist photographer. Link to my photography site http://himadriphotography.com/
This entry was posted in Commerce Connect, Sitecore and tagged , , , , . Bookmark the permalink.

One Response to Dynamic Multilist with Search

  1. Pingback: Custom Multisite Multilist with Search Sitecore field | Tech Musingz

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