Updating a Column Across All Items Within a List

So while working on a custom WebPart I recently needed to update a custom status field across all items within a list , this was a first for me so I explored the various options. 

The obvious approach is to just iterate the list updating each item individually and this could just be done as follows:

using(SPSite site= newSPSite(“SHAREPOINT_URL”))
using(SPWeb web= site.OpenWeb())
// get list
        SPList myList = web.Lists[“LIST_NAME”];

        ItemCollection = myList .Items;      

        // iterate list items
        foreach (SPListItem i in ItemCollection)
            // perform action on individual list items here


There is a reasonably noticeable performance hit when using this approach, even on smallish lists.  So I decided to explore batch processing and found a brief but handy Microsoft page (Batch Updating List Items in Windows SharePoint Services 3.0) providing an example of using the SPWeb.ProcessBatchData Method.  An example of which is below:

StringBuilder methodBuilder = newStringBuilder();
string batch = string.Empty;

string batchFormat = “<?xml version=\”1.0\” encoding=\”UTF-8\”?><ows:Batch OnError=\”Return\”>{0}</ows:Batch>”;

string methodFormat = “<Method ID=\”{0}\”>” +
“<SetList>{1}</SetList>” +
“<SetVar Name=\”Cmd\”>Save</SetVar>” +
“<SetVar Name=\”ID\”>{2}</SetVar>” +
“<SetVar Name=\”urn:schemas-microsoft-com:office:office#FIELD_NAME\”>{3}</SetVar>” +

using (SPSite site = newSPSite(“SHAREPOINT_URL”))
using(SPWeb web= site.OpenWeb())
        // get list
        SPList myList = web.Lists[“LIST_NAME”];
        string listGuid = myList.ID.ToString();

        // get list items to work with
        SPListItemCollection unprocessedItems = myList.Items;

        // build caml commands
        for (int i = 0; i < unprocessedItems.Count; i++)
            // get item id
int itemID = unprocessedItems[i].ID;  

            methodBuilder.AppendFormat(methodFormat, itemID, listGuid, itemID, “FIELD_VALUE”);

        // put the pieces together.
        batch = string.Format(batchFormat, methodBuilder.ToString());

        // process batch commands.
        string batchReturn = web.ProcessBatchData(batch);

The above approach is certainly quicker that manually iterating the list but one important observation I have made is that is that the code will continue to execute immediately after calling the .web.ProcessBatchData() method, this may or not be important depending on what you are doing.  For example at this point I wanted to update the status field for all list items simply to indicate that another background job being handled by a windows service had been initiated that would process on every list item.  The job being handled by the windows service would update the status field again once the list item had been processed.  The problem here is that it’s quite possible that the code will continue to execute and the status could be changed by my job before the batch command has been processed.  As far as I have been able to establish it isn’t possible to monitor the progress of the batch commands as it is just passed to SharePoint to handle.

If anyone knows if there is anyway to monitor the progress of the batch commands that have been handled I would love to know, sadly I don’t think this is possible meaning that this approach is not going to be suitable for all situations.

This entry was posted in SharePoint. Bookmark the permalink.

One Response to Updating a Column Across All Items Within a List

  1. Pingback: Updating SharePoint Managed Metadata Columns with PowerShell | MSDN Blogs

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 )

Google+ photo

You are commenting using your Google+ 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