none
Remove an item from SPFieldLookupValueCollection RRS feed

  • Question

  • 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.

    Friday, August 2, 2013 7:44 AM

Answers

  • Most likely the problem is with the comparison. The object stored in the array (objLookupFieldValueCol) has some difference compared to the new object you are creating (lookupValue) and therefore it is considered different and not removed.

    Cheers


    Alex Ferreira :: Twitter: @alexaem :: Founder of Get-SP.com :: Twitter: @GetSPcom

    • Proposed as answer by Ferreira, Alex Thursday, August 15, 2013 10:27 AM
    • Marked as answer by That.Guy Friday, August 16, 2013 1:59 PM
    Wednesday, August 14, 2013 5:10 PM

All replies

  • Hi,

    Can you explain what goal you are trying to achieve?

    Have you tried debugging the code? Is it iterating any item at all?

    Cheers


    Alex Ferreira :: Twitter: @alexaem :: Founder of Get-SP.com :: Twitter: @GetSPcom

    Friday, August 2, 2013 10:46 AM
  • Hi Alex,

    i have an event receiver attached with list 'TestA'. when i update an item in 'TestA', it will check for the value of 'Department' in list 'TestB'. If the same department is already there, i should remove the Item(which is updated) from the "Employee" look up column. Simply, i just wanted to remove an item from SPFieldLookupValueCollection.

    Yes, i debug the code. Query executes fine, SPFieldLookupValueCollection iterating the items but nothing happen on Remove(lookupvalue) 

    Friday, August 2, 2013 3:59 PM
  • Can you try using:
    Item["Employee"] = objLookupFieldValueCol.ToString();
    Cheers

    Alex Ferreira :: Twitter: @alexaem :: Founder of Get-SP.com :: Twitter: @GetSPcom

    Friday, August 2, 2013 4:27 PM
  • i tried the code you mentioned, but unfortunately no result. i simplified my code to make it more understandable.

      using (SPWeb web = properties.OpenWeb())
                {
                    SPList TestB = web.Lists["TestB"];
                    SPListItemCollection Items = TestB.Items;
                    foreach (SPListItem Item in Items)
                    {
                        SPFieldLookupValueCollection    objLookupFieldValueCol = (SPFieldLookupValueCollection)Item["Employee"];
                        SPFieldLookupValue lookupValue = new SPFieldLookupValue();
                        lookupValue.LookupId = 19;
                        objLookupFieldValueCol.Remove(lookupValue);
                        Item["Employee"] = objLookupFieldValueCol.ToString();
                        Item.Update();
    
                    }
    }

    i wanted to remove the item in Multi look up with id=19

    Saturday, August 3, 2013 10:56 AM
  • The Remove() method returns a boolean. Can you check what is returning?

    Alex Ferreira :: Twitter: @alexaem :: Founder of Get-SP.com :: Twitter: @GetSPcom

    Wednesday, August 14, 2013 5:07 PM
  • Most likely the problem is with the comparison. The object stored in the array (objLookupFieldValueCol) has some difference compared to the new object you are creating (lookupValue) and therefore it is considered different and not removed.

    Cheers


    Alex Ferreira :: Twitter: @alexaem :: Founder of Get-SP.com :: Twitter: @GetSPcom

    • Proposed as answer by Ferreira, Alex Thursday, August 15, 2013 10:27 AM
    • Marked as answer by That.Guy Friday, August 16, 2013 1:59 PM
    Wednesday, August 14, 2013 5:10 PM
  • Hi Rizwan,

    In your code, the way you are creating the variable "lookupValue" is wrong. Try the following code -

    using (SPWeb web = SPContext.Current.Web)
                {
                    SPList TestB = web.Lists["TestB"];
                    SPListItemCollection Items = TestB.Items;
                    foreach (SPListItem Item in Items)
                    {
                        int removalIndex = 0;
                        int count = 0;
                        SPFieldLookupValueCollection objLookupFieldValueCol = (SPFieldLookupValueCollection)Item["Employee"];
                        foreach (SPFieldLookupValue value in objLookupFieldValueCol)
                        {
                            if (value.LookupId == 19)
                                removalIndex = count;
                            count++;
                        }
                        objLookupFieldValueCol.RemoveAt(removalIndex);
                        Item.Update();
                    }
                }

    Narahari

    ******If a post answers/helps your question, please click "Mark As Answer" on that post and/or "Vote as Helpful*******

    • Proposed as answer by Gaurav Kainth Wednesday, June 18, 2014 5:12 PM
    Thursday, August 15, 2013 3:28 AM
  • Try this

    objLookupFieldValueCol.Remove(objLookupFieldValueCol.Find(delegate(SPFieldLookupValue LV){return LV.LookupId == ItemID;}));

    Wednesday, September 11, 2013 2:10 PM