Second Life of a Hungarian SharePoint Geek

January 26, 2010

Updating multi value fields using web service call and batch update

Filed under: SharePoint, Web service — Tags: , — Peter Holpar @ 19:00

In my recent post I showed some examples about updating lookup (SPFieldLookup) and user (SPFieldUser) fields. The examples in that post worked with single value fields.

Updating multi value fields through web service calls or using batch updates is a bit different and a rather undocumented area of SharePoint development, so let’s see some examples about that.

I’ve altered the Tasks list we worked with in the past article to allow its Location field to have multiple value, furthermore I’ve added a multi value user field called Owners, and a multi value choice field called Choices with options Value1, Value2 and Value3. It means the value of the Location field will be of type SPFieldLookupValueCollection, the value of the Owners field will be of type SPFieldUserValueCollection, and the value of the Choices field will be of type SPFieldMultiChoiceValue.

The following code example shows how to update these fields using a batch that updates the list item having ID = 3 by calling the ProcessBatchData method.

  1. SPList list = web.Lists["Tasks"];
  2.  
  3. String batch = String.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
  4.     "<ows:Batch OnError=\"Continue\"><Method ID='1'>" +
  5.     "<SetList>{0}</SetList>" +
  6.     "<SetVar Name='Cmd'>Save</SetVar>" +
  7.     "<SetVar Name='ID'>3</SetVar>" +
  8.     "<SetVar Name='urn:schemas-microsoft-com:office:office#Location'>1;#;#2</SetVar>" +
  9.     "<SetVar Name='urn:schemas-microsoft-com:office:office#Owners'>1;#;#7<</SetVar>" +
  10.     "<SetVar Name='urn:schemas-microsoft-com:office:office#Choices'>Value1;#Value2</SetVar>" +
  11.     "</Method></ows:Batch>", list.ID);
  12.  
  13. String batchResult = list.ParentWeb.ProcessBatchData(batch);

The main point is that you should separate IDs in lookup and user fields with “;#;#”, but you should use only a simple “;#” in the case of the multi choice field. That is because we can omit the lookup value of the field value, and you should only specify the lookup ID. As we discussed in the former post, you can specify an arbitrary lookup value if you wish, like “1;#some value;#2;#other value” or “1;#user1;#7;#user7” it has no effect to the update.

An important note about an issue I’ve experienced when playing with the code. As you may have already noticed I prefer using apostrophes to double quotation marks as they make it possible to create more readable code. If you replaces the quotes in the XML header tag as well as shown below:

<?xml version=’1.0′ encoding=’UTF-8′?>

You will get an exception that states:

Value does not fall within the expected range.

At first one can think it is because of a misspelled field name but the real reason is quite different. So keep using quotes in the header.

The following code illustrates the very same concept using UpdateListItems method of the Lists web service, the only difference is that in this case we update the list item having ID = 2.

  1. MyListService.Credentials = CredentialCache.DefaultCredentials;
  2. MyListService.Url = "http://yourserver/_vti_bin/lists.asmx&quot;;
  3.  
  4. XmlDocument updateRequest = new XmlDocument();
  5.  
  6. String updateBatch = "<Batch OnError='Continue'>" +
  7.                         "<Method ID='1' Cmd='Update'>" +
  8.                         "<Field Name='ID'>2</Field>" +
  9.                         "<Field Name='Location'>1;#;#2</Field>" +
  10.                         "<Field Name='Owners'>1;#;#7</Field>" +
  11.                         "<Field Name='Choices'>Value1;#Value2</Field>" +
  12.                         "</Method>" +
  13.                         "</Batch>";
  14.  
  15. updateRequest.LoadXml(updateBatch);
  16.  
  17. XmlNode deleteResult = MyListService.UpdateListItems("Tasks", updateRequest.DocumentElement);

Advertisements

7 Comments »

  1. I just wrote a script using UpdateListItems and multiple lookups, on wss v3. For me a single ;# worked fine, no need to double it…

    Comment by Christophe — April 13, 2010 @ 09:35

  2. Hi

    I have to update a list item dropdownlist, it gets updated and also reflect the changes in the view mode but when try to edit the list in the edit mode , it does not select the updated dropdownlist option.Let me know how to resolve this.

    Thanks

    Comment by megha — August 16, 2010 @ 22:38

    • Hi megha,

      Well, that is not strongly bound to the post, but I try to help. I don’t know what method (web service, object model) do you use, but it is the most likely that the value you set do not match exactly with any of the values in the select list (for example there is an extra space or other typo). I recommend you to set the value on the UI, get the value from code, next set your value from code, get the value again from code and compare the values you got byte-by-byte.

      Hope it helps.

      Peter

      Comment by pholpar — August 17, 2010 @ 09:39

  3. Thanks a lot

    Comment by megha — August 18, 2010 @ 20:11

  4. Hi
    thanks for you helpful post.
    I’m using this web service now,but I have some different kind of problem!
    I have some column names in my destination list in Persian , when I want to set these field, my workflow don’t do anything.I’m using “NEW” method of this web service.

    Comment by hamid — September 5, 2010 @ 07:46

  5. Thank you for a clear and timely reminder – is it possible to include an MSDN reference to how you came to learn that the lookup value can be omitted on SPLookup fields?

    Comment by mike cuffe — April 7, 2014 @ 03:43

    • Hi Mike, to tell the truth I don’t remember exactly, but most likely I simply played with sending various requests to the web service, and checked the behaviour in response. If there were any kind of MSDN reference, I would have already included a link in my post to that.

      Comment by Peter Holpar — April 7, 2014 @ 20:51


RSS feed for comments on this post. TrackBack URI

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 )

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: