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();
3

1 Answer 1

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. :)

2
  • Thanks for reply but What's getitemsomehow ??
    – NOOR
    Commented Apr 13, 2013 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
    Commented Apr 13, 2013 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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