Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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();
share|improve this question
    
I thought my question is very easy but it's important for me :( –  NOOR Apr 13 '13 at 20:04
    
I have edited your question (changed formatting). Please, approve this edit. It will help others in future. –  MikhailSP Apr 13 '13 at 20:56
add comment

1 Answer

up vote 4 down vote accepted

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

share|improve this 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
    
Thank you very much :) –  NOOR Apr 13 '13 at 20:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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