SharePoint Lookup Field Update

When working with SharePoint list you should often update field values, like lookup fields (SPFieldLookup) and user fields (SPFieldUser, that is a subclass of the former one). In these cases you have to work with value types of SPFieldLookupValue (or SPFieldLookupValueCollection for multi valued fields) or SPFieldUserValue (SPFieldUserValueCollection for multi valued fields).

If you want to update these type of fields, you should know the ID of the referenced list item or user.

You can do the same when updating the items using the object model.

SPList list = web.Lists[“Product Details”];

item[“Product Category ID”] = “SCAN-001”;

item[“Product ID”] = new SPFieldLookupValue(1, null); // Here value 1 is ID of the referenced list item

item[“Product Owner”] = new SPFieldUserValue(web, 1, null); // Here value 1 is ID of the referenced list user

item.Update();

Leave a Reply

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