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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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