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();

Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: