1
votes

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
the code above is actually contained inside another for loop that loops through the SPListItemCollection from the lookup-listelizabeth
Bah! I found the problem - it was a silly one too. On the outer for loop, i++...on the inner for loop j++. When I looped through the array of selections from the listbox - I used the 'i' index instead of the 'j' index. D'oh! I appreciate your site -- usually when I run into a snag...I can find the answer here. Thank you.elizabeth

1 Answers

1
votes

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.