Updating choice fields via SharePoint Web Services

**May 20, 2010 Update

** This post is about using a C# console app to update choice fields via SharePoint web services.  If you are looking for more information on how SharePoint web services can be called via JavaScript, check out my post on building a simple SharePoint AJAX app.


My problem started with a simple change to a site column. I had originally created a Country field with the following values:

  • US
  • Canada

After a change in requirements, the new values for the field were:

  • Canada
  • Germany
  • Great Britain
  • Ireland
  • United States

After updating the site column, I then needed to update the hundreds of items in the list to use “United States” instead of “US”. Instead of updating hundreds of items manually, I decided to write a small web services console app. (I was also not able to update it in datasheet… due to content approval maybe???)

However, I quickly ran into problems when attempting to update the multi-select choice field.

The Setup:

My site column was defined with each choice for Country in alphabetical order:

2009-10-30 10 24 59

Symptoms:

The symptoms of my problem were very strange. After running my app successfully, it seemed as if my updates were simply ignored by the server.

  1. The item is successfully updated using the Lists.asmx (it even creates a new version!)
  2. The item does not show the updated value for your multi-select choice field in the UI.

The Code:

Here is the very simple code that is creating the update message:

private static void AddCountryUpdate(ref StringBuilder sb, int count, string id, string newCountry)
{
    sb.AppendFormat("", count);
    sb.AppendFormat("{0}", id);
    sb.AppendFormat("{0}", newCountry);
    sb.Append("");
}

Calling the following code to update:

XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.InnerXml = sb.ToString();
XmlNode ndReturn = list.ListWebSvc.UpdateListItems(listName, elBatch);

The Results:

Attempting to update Country to be “;#United States;#Canada;#” resulted in a “success” response (error code of 0x00000000) even when the update was NOT successful:

ID="373,Update">
  >
0x00000000>
  ows_ID="1311" ows_Country=";#United States;#Canada;#" ows_Modified="2009-10-29 16:01:05" ows__UIVersionString="6.0" />
>

Viewing this in the UI still shows the old value:

Country - Old Value

However, only after the values are in order, “;#Canada;#United States;#”, will the update actually happen.

ID="223,Update">
  >
0x00000000>
  ows_ID="1311" ows_Country=";#Canada;#United States;#" ows_Modified="2009-10-30 08:51:45" ows__UIVersionString="7.0" />
>

Viewing this in the UI shows the updated value:

Country - Correct

Notice it still created a version 6.0 and 7.0:

Version History

Conclusion:

When updating a multi-select choice field using SharePoint’s Lists.asmx web service, the order of the choices in the update message are important. If they are out of order, it will not update.