Second Life of a Hungarian SharePoint Geek

January 26, 2010

Updating SharePoint lookup and user field values

Filed under: SharePoint — Tags: — Peter Holpar @ 00:21

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 only. Jinal Patel shows an example for that in this post using the UpdateListItems method of the Lists web service.

You can do the same when updating the items using the object model either through updating an individual item or using the batch update method.

For the sake of this example I’ve added a single value lookup field called Location to a Task list on my site. This field refers to another list of the same site that contains city names in the referenced field.

The following code updates the Location field (SPFieldLookup) and the AssignedTo field (SPFieldUser) of the task list item having ID = 1.

  1. SPList list = web.Lists["Tasks"];
  2. SPListItem item = list.Items.GetItemById(1);
  3. item["Location"] = new SPFieldLookupValue(1, null);
  4. item["AssignedTo"] = new SPFieldUserValue(web, 1, null);
  5. item.Update();

Including the name part of the lookup field has really no effect; it is simply ignored, for example:

item["Location"] = new SPFieldLookupValue(1, "abcdefgh");

The above code has the same result as using null as the lookup name, even if the name part is not matching the real value of the remote field.

You can do the same using the ProcessBatchData method. The following example updates the task list item having ID = 3.

  1. String batch = String.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  2.     "<ows:Batch OnError=\"Continue\"><Method ID='1'>" +
  3.     "<SetList>{0}</SetList>" +
  4.     "<SetVar Name='Cmd'>Save</SetVar>" +
  5.     "<SetVar Name='ID'>3</SetVar>" +
  6.     "<SetVar Name='urn:schemas-microsoft-com:office:office#Location'>2</SetVar>" +
  7.     "<SetVar Name='urn:schemas-microsoft-com:office:office#AssignedTo'>1</SetVar>" +
  8.     "</Method></ows:Batch>", list.ID);
  9. String batchResult = list.ParentWeb.ProcessBatchData(batch);

Including the name part of the lookup field has no effect as in the case of the single item update above.

<SetVar Name=’urn:schemas-microsoft-com:office:office#Location’>2;#abcdefgh</SetVar>

This would be identical with the previous version of batch update without the name part, even if the name part is not matching the real value of the remote field.

This behavior of the lookup and user fields is very nice, as if it required the name part as well would mean we should do an extra lookup in the remote list for the value of the referenced field.


  1. […] Second Life of a Hungarian SharePoint Geek If your sword is too short, take one step forward « Updating SharePoint lookup and user field values […]

    Pingback by Updating multi value fields using web service call and batch update « Second Life of a Hungarian SharePoint Geek — January 26, 2010 @ 19:00

  2. Hi How will you insert a new User using the SP List web service. The column type is “User”. I am inserting like “-1;#. If the UerID is new, then am getting User doesnot exist or it should be unique.How to Overcome this?

    Comment by Guru — February 22, 2010 @ 08:46

  3. Hi,

    AFAIK, you cannot insert new users through the Lists web service. When you update an item as described in this post, you only update a reference to an existing user, but do not create a new one.

    I really don’t see, how would you like to create a new user simply by inserting “-1;#”.

    To create a new user on the site, you should use the AddUserToRole method ( or the AddUserToGroup method ( of the Users and Groups Web Service.

    After creating the user, you should call the GetUserInfo method ( passing the login name as parameter, and use the ID in the response to insert / update the user field in your list items.


    Comment by pholpar — February 23, 2010 @ 00:03

  4. An important point of clarification. The Field Name (i.e., urn:schemas-microsoft-com:office:office#Location) must be the Internal Name and not the Display name of the field.

    Thanks for the solution.

    Comment by Ben — November 23, 2011 @ 07:16

  5. Hi pholpar,

    Thanks for your post: It saved me a bit of time.🙂

    Comment by Stu — May 1, 2012 @ 09:18

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

You are commenting using your 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

Create a free website or blog at


Get every new post delivered to your Inbox.

Join 64 other followers

%d bloggers like this: