Updating SharePoint lookup and user field values

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.

Tags:

4 Responses to “Updating SharePoint lookup and user field values”

  1. Updating multi value fields using web service call and batch update « Second Life of a Hungarian SharePoint Geek Says:

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

  2. Guru Says:

    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?

  3. pholpar Says:

    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 (http://msdn.microsoft.com/en-us/library/ms774883.aspx) or the AddUserToGroup method (http://msdn.microsoft.com/en-us/library/ms772683.aspx) of the Users and Groups Web Service.

    After creating the user, you should call the GetUserInfo method (http://msdn.microsoft.com/en-us/library/ms774637.aspx) passing the login name as parameter, and use the ID in the response to insert / update the user field in your list items.

    Peter

  4. Ben Says:

    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.

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 )

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


Follow

Get every new post delivered to your Inbox.

Join 34 other followers