Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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
        {
            //SPListItem NewlistItem = TestB.AddItem();
            SPFieldLookupValue lookupValue = new SPFieldLookupValue();
            lookupValue.LookupId = ItemID;
            SPFieldLookupValueCollection objLookupFieldValueCol = new SPFieldLookupValueCollection();
            objLookupFieldValueCol.Remove(lookupValue);

            //NewlistItem["Employee"] = objLookupFieldValueCol;
            //NewlistItem["Department"] = department;

            //NewlistItem.Update();
        }
        web.AllowUnsafeUpdates = false;
    }
}

it is not removing any value from the lookup column.
Posted

1 solution

Try this
C#
objLookupFieldValueCol.Remove(objLookupFieldValueCol.Find(delegate(SPFieldLookupValue LV){return LV.LookupId == ItemID;}));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900