4

I have a list in SharePoint and it contains a lookup field. Now I want to insert an item to this list, but don't know how to work with the lookup field. I tried the following code but no item is added.

SPFieldLookupValue gov = new SPFieldLookupValue(gv.SelectedValue);
items["gov_name"] = gov.LookupValue;
items.Update();
6

Please, take a look at this question. So you can do something like this:

SPListItem lookedUpItem =  GetItemSomeHow();
newItem[lookupField] = new SPFieldLookupValue( lookedUpItem.ID, lookedUpItem.Title);
newItem.Update();

Hope it helps. If so, please, don't forget to press upvote and mark as answer. :)

  • Thanks for reply but What's getitemsomehow ?? – NOOR Apr 13 '13 at 20:33
  • this means that you need to obtain an SPListItem object you want to lookup to. For instance it could look like: SPListItem lookedUpItem=web.Lists["SomeList"].Items[1] for the item with ID=1 in the list "SomeList"; – MikhailSP Apr 13 '13 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.