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

1 Answer 1

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.

9
  • 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
    Commented Sep 12, 2018 at 13:08
  • Do you already have the sort name you want to add to the sorting field? Commented Sep 12, 2018 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
    Commented Sep 12, 2018 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 Commented Sep 12, 2018 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
    Commented Sep 12, 2018 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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