2

I'm trying to update a droplink field value using its name but I get the following error:

Value not in the selection list

The code is

item.Fields["__Subitems Sorting"].Value = sort; // sort contains the name ("Reverse")

As far as I understand, they expect me to pass the ID instead of the name but how can i retrieve it from the name?

Thank you in advance for your help

3

The subitem sorting has the datasource pointing to the path /sitecore/system/Settings/Subitems Sorting.

Since you have the name of the sorting type, you can find the item from the container and then assign it to the __Subitems Sorting field.

public void SetSorting(Item item, string sortType)
{
    var sortItem = Factory.GetDatabase("master").GetItem(new ID("{B01EB085-82BC-4D79-B15F-90EAB9E98062}")).Children
        .FirstOrDefault(w => w.Name.Equals(sortType));

    if (sortItem != null)
    {
        item.Editing.BeginEdit();
        item.Fields["__Subitems Sorting"].Value = sortItem.ID.ToString();
        item.Editing.EndEdit();
    }
}

The ID {B01EB085-82BC-4D79-B15F-90EAB9E98062} is the Sitecore Id for the item Subitems Sorting. You can use the path if you want.

  • I took inspiration out of your code : var sortItem = Factory.GetDatabase("master").GetItem(item.Fields["__Subitems Sorting"].ID).Children .FirstOrDefault(w => w.Name.Equals(sort)); and it seems that children is empty . Do you know what could cause this problem ? – Slrg Sep 12 '18 at 13:08
  • Do you already have the sort name you want to add to the sorting field? – Hishaam Namooya Sep 12 '18 at 13:14
  • Yes i want to use "Reverse" for example. That's what the sort variable contains. I debugged it with a quick watch. It seems the problem comes from Children. It contains nothing – Slrg Sep 12 '18 at 13:18
  • So, you will need to first get the item. All the options are found under the path /sitecore/system/Settings/Subitems Sorting. Try to use the above code as is because the sort variable that you have already has the same name as the one from the list of items under the Subitems Sorting – Hishaam Namooya Sep 12 '18 at 13:20
  • But yes, that's what i want! i want to select "Reverse" which already exists in the droplink field. In your message, you are suggesting to split the logic in 2 lines ? first i get the item and then another line for getting the "Reverse" Item ? – Slrg Sep 12 '18 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.