Search code examples
sitecoretreelist

How to select item of treelist in sitecore programmatically


I have a office location treelist in a content item, programmatically I have to select a location. How to do it... any information


Solution

  • The way we set a text value to a sitecore field programatically similarly it is for multilist or treelist any field. Only difference is in treelist and multilist content ids are pipeline separated e.g. {GUID for office location 1}|{GUID for office location 2}

    So you need to edit the treelist field like below:

    using (new Sitecore.SecurityModel.SecurityDisabler())
    {
    
      Item item = master.GetItem("path of your item");
      if (item !=null)
      {
        item.Editing.BeginEdit();
        item["Office Location Treelist fieldname"] = "{40764AF5-F3C9-4B36-8B51-3EF36702E3DB}|{406200EB-E5D5- 
                                    47FB-9031-8A49A7C8FC06}";   
                     // Make sure these item ids belong to datasource of your treelist
        item .Editing.EndEdit();
      }