Skip to main content

Access Checklist, Multilist, Treelist, and TreelistEx fields


How to access Checklist, Multilist, Treelist, and TreelistEx fields using the Sitecore.Data.Fields.MultilistField class.

You can access field types allowing the user to select multiple items, including Checklist, Multilist, Treelist, and TreelistEx, by using the Sitecore.Data.Fields.MultilistField class.

You can use the Sitecore.Data.Fields.MultilistField.GetItems() method to access a list of Sitecore.Data.Item.Item objects representing the items specified by the field. For example, the following C# code shows you how to iterate over the items specified in the Multilist field named related.

public ActionResult Index()
     DogModel myDog;
     var dogs = new List<DogModel>();

     //Get the current page using Sitecore Item API
     var currentPage = Sitecore.Context.Item;

      //Cast to a Sitecore item
     Sitecore.Data.Items.Item myItem = (Sitecore.Data.Items.Item)currentPage;

     //Read the Multifield List
     Sitecore.Data.Fields.MultilistField multiselectField = myItem.Fields["related"];

    Sitecore.Data.Items.Item[] items = multiselectField.GetItems();

    //Iterate through each item
    if (items != null && items.Length > 0)
          for (int i = 0; i < items.Length; i++)
            //Allocate memory to the model
             myDog = new DogModel();

             Sitecore.Data.Items.Item dogItem = items[i];
    = FieldRenderer.Render(dogItem, "Name");
             myDog.long_description = FieldRenderer.Render(dogItem, "LongDescription");
             myDog.breed = FieldRenderer.Render(dogItem, "Breed");

              //Get the link to the page
               var myLink = Sitecore.Links.LinkManager.GetItemUrl(dogItem);

              //Need to check and if /en is there - remove it 
               if (myLink.StartsWith("/en"))
                 //remove it
                    myLink = myLink.Substring(myLink.LastIndexOf("/Dogs"));
              myDog.uri = myLink; 

               //push to the list
           //Return the collection to the view
            return View(dogs);

The individual members of the list returned by Sitecore.Data.Fields.MultilistField.GetItems() method are never Null. If a user has deleted an item without updating the references to that item, the Sitecore.Data.Fields.MultilistField.GetItems() method excludes that item from its results.

You can also use the Sitecore.Data.Fields.MultilistField class to access fields of type Droplink, Droptree, and Grouped Droplink. This approach provides you with a single programming model for all field types that store the IDs of one or more Sitecore items, and could reduce the need to update code if you change the type of the field. Because Droplink, Droptree, and Grouped Droplink do not support selection of multiple items, you should not use the Sitecore.Data.Fields.Multilist class to update these types of fields.

You can add items to a supported field type by using the Sitecore.Data.Fields.MulitlistField.Add() method, and remove items by using the Sitecore.Data.Fields.MulitlistField.Remove() method. For example, to ensure that the TreelistEx Multiselect field in the /Sitecore/Content/Home item in the Master database specifies the /Sitecore/Content/Home/Sample1 item, but does not specify the /Sitecore/Content/Home/Sample2 item:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Items.Item sample1 = master.GetItem("/sitecore/content/home/sample1");

Sitecore.Data.Items.Item sample2 = master.GetItem("/sitecore/content/home/sample2");

Sitecore.Data.Fields.MultilistField multiselectField = home.Fields["multiselect"];

if(multiselectField.Contains(sample2.ID.ToString()) || !multiselectField.Contains(sample1.ID.ToString()))