Hi I have two Lists in sharepoint 2007.
I have a lookup column in on list which looks the other field.
I want to use the sharepoint object model to add an item to the second list.
How to i set the lookup field value. (The value is already in the other list).?
SPListItem Employee = web.Lists["Employee"].Items.Add();
Employee["Name"] = account.Name;
Employee["Department"] = <lookup value must come here>
Employee.Update();
Best Answer
Lookup fields will contain a combination of the row's id and the value of the column to display, separated by
:#
, in your case that could be1:#HumanResources
or12:#Engineering
.So to reference a lookup simply setting the id won't be enough, instead the above mentioned string needs to be set. Luckily SharePoint provides the class
SPFieldLookupValue
that does exactly this: