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();
MikhailSP
  • 1,449
  • 2
  • 13
  • 26
NOOR
  • 53
  • 1
  • 1
  • 6

1 Answers1

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

MikhailSP
  • 1,449
  • 2
  • 13
  • 26
  • 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