Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

Remove an item from SPFieldLookupValueCollection

$
0
0

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.


Viewing all articles
Browse latest Browse all 25064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>