Insert/Update multiple lookup values to an SPListItem

4.6k views Asked by At

My SharePoint list has a column that allows multiple lookup values. My C# control (within a webpart) allows the user to make multiple selections from a list box. I split these values into an array - each array member being a selected value that needs to be updated in the same SPListItem column.

I know that the selections are being passed in properly from the list box - I just need to add this group of values to the same column in the SPListItem.

Where am I going wrong?

SPFieldLookupValueCollection MyCollection = new SPFieldLookupValueCollection();
for (int i = 0; i < MyArrayOfSelections.Length; i++)
{
   if (MyLookupList["LookupColumn"].ToString() == MyArrayOfSelections[i].ToString())
   {
      MyID = int.Parse(MyLookupList[i]["ID"].ToString());
      SPFieldLookupValue thisSelection = new SPFieldLookupValue(MyID,MyArrayOfSelections[i].ToString());
      MySubCollection.Add(thisSelection);
      }
   }
   ListIWantToUpdate["ColumnWithMultipleLookupSelections"] = SubCollection;
   ListIWantToUpdate.Update();
   site.Update();
}
1

There are 1 answers

1
naivists On

The last rows of the code example are confusing (maybe it's just variable naming). If you are just updating the data, you never need to update neither the SPList object (this requires "Manage lists" permission on the particular list, nor SPSite ojbect (requires you to be site administrator or owner). So, this code will not run succcessfuly for a regular user.