I have a multilookup column "Employee". i wanted to remove one value from the column if some specific condtion is true. Below is the code:
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
using (SPWeb web = properties.OpenWeb())
{
SPList TestA = web.Lists["TestA"];
string employee = Convert.ToString(properties.ListItem["EmployeeNo"]);
string department = Convert.ToString(properties.AfterProperties["Department"]);
int ItemID = properties.ListItem.ID;
SPList TestB = web.Lists["TestB"];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Department' /><Value Type='Text'>" + department + "</Value></Eq></Where>";
SPListItemCollection items = TestB.GetItems(query);
if (items.Count > 0)
{
foreach (SPListItem Item in items)
{
web.AllowUnsafeUpdates = true;
SPFieldLookupValueCollection objLookupFieldValueCol = (SPFieldLookupValueCollection)Item["Employee"];
SPFieldLookupValue lookupValue = new SPFieldLookupValue();
lookupValue.LookupId = ItemID;
objLookupFieldValueCol.Remove(lookupValue);
Item["Employee"] = objLookupFieldValueCol;
Item.Update();
web.AllowUnsafeUpdates = false;
}
}
else
{
//DO NOTHING
}
web.AllowUnsafeUpdates = false;
}
}
it is not removing any value from the lookup column.