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.
- SPList list = web.Lists["Tasks"];
- SPListItem item = list.Items.GetItemById(1);
- item["Location"] = new SPFieldLookupValue(1, null);
- item["AssignedTo"] = new SPFieldUserValue(web, 1, null);
- 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.
- String batch = String.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
- "<ows:Batch OnError=\"Continue\"><Method ID='1'>" +
- "<SetList>{0}</SetList>" +
- "<SetVar Name='Cmd'>Save</SetVar>" +
- "<SetVar Name='ID'>3</SetVar>" +
- "<SetVar Name='urn:schemas-microsoft-com:office:office#Location'>2</SetVar>" +
- "<SetVar Name='urn:schemas-microsoft-com:office:office#AssignedTo'>1</SetVar>" +
- "</Method></ows:Batch>", list.ID);
- 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: SharePoint
January 26, 2010 at 19:00 |
[...] Second Life of a Hungarian SharePoint Geek If your sword is too short, take one step forward « Updating SharePoint lookup and user field values [...]
February 22, 2010 at 08:46 |
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?
February 23, 2010 at 00:03 |
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
November 23, 2011 at 07:16 |
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.