Updating choice fields via SharePoint Web Services
By Kit
**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:
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.
- The item is successfully updated using the Lists.asmx (it even creates a new version!)
- 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:
{
sb.AppendFormat("
sb.AppendFormat("
sb.AppendFormat("
sb.Append("");
}
Calling the following code to update:
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:
>
Viewing this in the UI still shows the old value:
However, only after the values are in order, “;#Canada;#United States;#”, will the update actually happen.
>
Viewing this in the UI shows the updated value:
Notice it still created a version 6.0 and 7.0:
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.