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();
}
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, norSPSite
ojbect (requires you to be site administrator or owner). So, this code will not run succcessfuly for a regular user.