0

My Code is something like this

MultilistField multilistField= item.Fields["FieldID"];
 foreach (var item in multilistField.GetItems())
    {
        if (multilistField.Contains(item.ID.ToString()))
       {
            //Code is not going inside, not able to understand why
             multilistField.Remove(item.ID.ToString());
         }
}

When I checked values in Field, I can see value with guid id in lower case without braces ({}) something like this

48afc299-9218-4187-a912-d7272698b163 while item.ID.toString() is in {48AFC299-9218-4187-A912-D7272698B163}.

3 Answers 3

0

After getting your code on my local and testing the scenario, I found that the reason maybe you have not selected anything in your Multilist Field.

The statement multilistField.GetItems() will give you the number of items that you have selected from your Multilist Field.

And when you run this statement

 multilistField.Remove(item.ID.ToString());

It will give you the error below.

Sitecore.Exceptions.EditingNotAllowedException: 'Item '/sitecore/content/Home/New test Page' is not in editing mode. Item ID: {56305117-8C9C-4C08-96D5-BB97A8E27419}'

For that, you have to use BeginEdit and EndEdit.

Refer to this link to learn how to remove items from Multilist Field.

https://sitecore.alexiasoft.nl/2007/08/29/remove-an-item-for-a-multilist/

0

Below code helped me in solving the issue. So basically Multilist field is having some D Type guids and it was not getting removed.

MultilistField field = item.Fields["fieldId"];
 foreach (var multiListItems in field.GetItems())
                            {
                                var dTypeGuid = multiListItems.ID.ToGuid().ToString("D");
                                if (field.Contains(dTypeGuid))
                                {
                                    field.Remove(dTypeGuid);
                                }
                                else if (field.Contains(multiListItems.ID.ToString().ToUpper()))
                                {
                                    field.Remove(multiListItems.ID.ToString().ToUpper());
                                }
                                else
                                {
                                    field.Remove(multiListItems.ID.ToString());
                                }
                            }
````
0

You have to use BeginEdit and EndEdit to remove items from Multilist Field

using (new Sitecore.SecurityModel.SecurityDisabler())
    {
      itm.Editing.BeginEdit();
      
     // You can write your code logic here 
      Sitecore.Data.Fields.MultilistField multiListField = 
      itm.Fields[“multiListFieldName”];
      multiListField.RemoveLink(item.ID.ToString());
     
     itm.Editing.EndEdit();
    }

You can refer this link as well for get more information about place an item in editing mode https://doc.sitecore.com/xp/en/developers/92/sitecore-experience-manager/place-an-item-in-editing-mode.html

Hope this helps you.

Your Answer

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

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