Reputation: 29674
I can't seem to find any documentation on this. I have an item, that has a TreelistEx
field:
I want to programatically add a new item to this field, preferably using glass mapper but vanilla sitecore will do if needs must.
Can anyone help me out here? How do I go about this?
You'll have to excuse the lack of details, I can't find a single thing to point me in the right direction here.
Upvotes: 0
Views: 337
Reputation: 1435
The TreeList and TreeListEx work the same way as the multilist.
using (new Sitecore.SecurityModel.SecurityDisabler())
{
Item newItem = Sitecore.Context.Item;
newItem.Editing.BeginEdit();
MultilistField mlf = newItem.Fields["FieldName"];
// adding an item
mlf.Add(ItemToAdd.ID.ToString());
// removing an item
mlf.Remove(ItemToRemove.ID.ToString());
newItem.Editing.EndEdit();
}
Upvotes: 1
Reputation: 29674
It seems that sitecore stores these fields as a piped separated list. So you can edit the values as a string (this is very stringly typed). So can manipulate the values as a string (god this is crappy). So to add an item into the treelistEx
that has a id of {652FD742-AAE3-468D-81BE-7EF18E06B796}
you can:
Item item;
item.Editing.BeginEdit();
item.Fields["fieldname"].Value += "|{652FD742-AAE3-468D-81BE-7EF18E06B796}";
item.Editing.EndEdit();
It's pretty simple to split the string into an array, manipulate and turn it back to a string.
TBH this code is pretty crappy. I'd like to think (knowing sitecore I'm not holding my breath) that there is a better way to achieve this...
Upvotes: 0