how to programmatically set a value of Treelist in

2019-07-24 08:02发布

问题:

I have a job description page, and I am adding values to it programmatically. One of the fields is "Office Locations", with a data type of Treelist. How can I add values to the Treelist so that I can set the office location?

回答1:

Sitecore Treelist stores the ids of selected items in a pipeline separated form.

If you want to set the value of Treelist programmatically you need to set it to a string which looks like:

{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}|{EA015A5F-C41B-4510-B538-438EF204F5E2}

where {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}, {EA015A5F-C41B-4510-B538-438EF204F5E2} and possibly more of the string parts separated with | characters are ids of chosen pages.

using (new EditContext(jobItem))
{
    jobItem["OfficeLocations"] = "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}|{EA015A5F-C41B-4510-B538-438EF204F5E2}";
}