Tuesday, November 10, 2009

Adding items manually into Lookup column in a list

I had little trouble adding multiple items into a lookup column in a list using SharePoint Object Model. Just thought of sharing the code here.

//Get the first item in the list ( just for example )
SPListItem taskItem = destinationList.Items[0];

//Get the reference to the lookup column for the selected item
//This lookup contains items which were selected in the lookup already
lookup = (SPFieldLookupValueCollection)taskItem["LookupColumnName"];

//Add a new lookup value 
docLookup.Add(new SPFieldLookupValue(id, name));

//Add the modified lookup back
taskItem["LookupColumnName"] = lookup;

Happy SharePoint learning!!!

No comments:

Post a Comment