Remove an item for a multilist

Sometimes you spent to many time on answering a question on SDN5 Forum(sorry for no code highlighting, CopySourceAsHtml won’t install):

Sitecore.Data.Items.Item itemToBeRemovedFromMultilist;

Sitecore.Diagnostics.Assert.IsNotNull(itemToBeRemovedFromMultilist, “No item selected to remove from multilist”);

Sitecore.Data.Items.Item contentItem = Sitecore.Context.ContentDatabase.GetItem(Sitecore.Context.Item.ID);

Sitecore.Diagnostics.Assert.IsNotNull(contentItem, “Couldn’t find item in ContentDatabase!”);

Sitecore.SecurityModel.Domain sitecoreDomain = Sitecore.Configuration.Factory.GetDomain(“sitecore”);

Sitecore.Diagnostics.Assert.IsNotNull(sitecoreDomain, “Couldn’t retrieve the Sitecore domain!”);

Sitecore.SecurityModel.UserItem adminUser = sitecoreDomain.GetUser(“admin”);

Sitecore.Diagnostics.Assert.IsNotNull(adminUser, “Couldn’t retrieve the admin-user from the Sitecore domain!”);

using (new SecuritySwitcher(adminUser))

{

contentItem.Editing.BeginEdit();

Sitecore.Data.Fields.MultilistField myMultilistField = contentItem.Fields[“myMultiListField”];

Sitecore.Diagnostics.Assert.IsNotNull(myMultilistField, “Field connot be retrieved, please check field name!”);

myMultilistField.RemoveLink(itemToBeRemovedFromMultilist);

contentItem.Editing.EndEdit();

}