SharePoint: Programmatically Setting Lookup Fields

Although when you get a lookup field, it returns a value ID;#LookupFieldValue (e.g. 45;#test@example.com), 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;
newItem.Update();

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>