SharePoint: Programmatically Setting Lookup Fields

Although when you get a lookup field, it returns a value ID;#LookupFieldValue (e.g. 45;, when you set it, you just pass it the id:

// To Get
SPWeb web = new SPSite("http://url/to/web").OpenWeb();
SPList list = web.Lists["listname"];
string lookupFieldValue = list.Items[itemIndex]["lookupFieldDisplayName"].ToString();
// To Set
SPWeb web = new SPSite("http://url/to/web").OpenWeb();
SPListItem newItem = web.Lists["listname"].Items.Add();
newItem["lookupFieldDisplayName"] = idOfAnItemInTheLookupTable;

You May Also Like

About the Author: rnowik

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.