Cascading (Auto-Populate) MultiList Field in Sitecore

Hello Sitecore Buddies! Thank you for accompanying Sitecore Journey. Feeling blessed to have such expert readers! Sitecore Journey is incomplete without all of you Sitecore expert readers! Well, Sitecore Journey is here today to share one of the unique requirements. Let’s see today’s scenario which will illustrate how to create custom field type in Sitecore. Scenario: There was a requirement to auto-populate one Sitecore Multilist field based on another Multilist Field in Sitecore – same like cascading dropdownlist of Ajax Toolkit in ASP.NET. Here’s the video which illustrates this requirement:

Solution: Many of you have already thought about how to achieve this requirement, yes you are correct – you need to create custom field type in Sitecore. So let’s create custom Multilist field type control which will populate/bind second Multilist field based on first Multilist’s selected values. Steps: 1) Create separate class library project in your Sitecore solution (if you have already such project then you can of-course use that). 2) Add a CustomMultilist class in this project, inherit CustomMultilist class from MultilistEx. Below is complete code snippet of CustomMultiList field control:

using Sitecore.Data.Items; using Sitecore.Shell.Applications.ContentEditor; using System; using System.Collections.Generic; using System.Linq; using System.Text;  namespace Sitecore.Extensions {     public class CustomMultiList : MultilistEx     {         protected override void DoRender(System.Web.UI.HtmlTextWriter output)         {             this.ExcludeItems();            base.DoRender(output);         }          private void ExcludeItems()         {                Item i = Sitecore.Context.ContentDatabase.GetItem(base.ItemID);              Sitecore.Data.Fields.MultilistField multilistField =i.Fields["MultilistField1"];             Item[] selectedItems = multilistField.GetItems();             StringBuilder sbItemId = new StringBuilder();             foreach (Item selectedItem in selectedItems)             {                 sbItemId.Append(selectedItem.Paths.FullPath).Append("|");                            }              if (sbItemId.Length > 0)             {                 this.Source = sbItemId.ToString().Remove(sbItemId.Length - 1, 1);             }                      }     } } 

3) Compile project and copy Sitecore.Extenstions.DLL in your [Sitecore Website Root]/bin directory. 4) Add below line in web.config of your Sitecore instance.

<source assembly="Sitecore.Extensions" mode="on" namespace="Sitecore.Extensions" prefix="contentExtension"></source> 

5) Create a new fieldtype (e.g. CustomMulitList field) in core database as shown in below image:

6)  Now go to your item template where you want to assign this CustomMultiList field. You should assign this CustomMultiList field to only that field which you want to auto-populate based one another MultiList field.

Enjoy your Sitecore Journey!

Leave a Reply