SharePoint In Action

An attempt to share my day-to-day SharePoint experience

Archive for the month “April, 2013”

Batch Updating The Items Of A SharePoint 2010 List

Hi all,

I created a web part for work last week to batch-update the items of a SharePoint 2010 list.
To make it simple I will show how to modify only one column in the whole list using CAML and you can simply expand the idea. My site is called “SampleSite” and the list is “SampleList”. We are modifying the column “SampleColumn”. Here is how the code looks like:

void UpdateList()
        {
            StringBuilder methodBuilder = new StringBuilder();
            string batch = string.Empty;
            string newValue="mmmm";
            string updateColumn = "SampleColumn";


            try
            {
                string batchFormat =    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                        "<ows:Batch OnError=\"Continue\">{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#{3}'>{4}</SetVar>" +
                                        "</Method>";
                
                using (SPSite siteCol = new SPSite("SampleSite"))
                {
                    using (SPWeb web = siteCol.OpenWeb())
                    {

                        // Get the list containing the items to update
                        SPList list = web.Lists["SampleList"];
                        string listGuid = list.ID.ToString();
                        SPListItemCollection allItems = list.GetItems();

                        // Build the CAML update commands.
                        for (int i = 0; i < allItems.Count; i++)
                        {
                            int itemID = allItems[i].ID;
                            methodBuilder.AppendFormat(methodFormat, itemID, listGuid, itemID, updatedColumn, newValue);
                        }
                        web.AllowUnsafeUpdates = true;

                        // Generate the CAML
                        batch = string.Format(batchFormat, methodBuilder.ToString());

                        // Process the batch 
                        string batchReturn = web.ProcessBatchData(batch);
                       
                    }
                    //done
                }
 
            }
            catch (Exception ex)
            {
		//show the error
            }

        }
}

Note how the reference to the field has been made in the CAML command:

<SetVar Name='urn:schemas-microsoft-com:office:office#{3}'>{4}</SetVar>

Also note that you would need to set the web.AllowUnsafeUpdates to true.

Assuming SampleList has 4 items, your generated CAML should look like this:

<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:Batch OnError=\"Continue\">
	<Method ID='1' ><SetList>6deeec64-95f8-4e26-b5ad-e8770deaae5c</SetList><SetVar Name='Cmd'>Save</SetVar> <SetVar Name='ID'>1</SetVar><SetVar Name='urn:schemas-microsoft-com:office:office#SampleColumn'>mmmm</SetVar></Method>

<Method ID='2' ><SetList>6deeec64-95f8-4e26-b5ad-e8770deaae5c</SetList><SetVar Name='Cmd'>Save</SetVar> <SetVar Name='ID'>2</SetVar><SetVar Name='urn:schemas-microsoft-com:office:office#SampleColumn'>mmmm</SetVar></Method>

<Method ID='3' ><SetList>6deeec64-95f8-4e26-b5ad-e8770deaae5c</SetList><SetVar Name='Cmd'>Save</SetVar> <SetVar Name='ID'>3</SetVar><SetVar Name='urn:schemas-microsoft-com:office:office#SampleColumn'>mmmm</SetVar></Method>

<Method ID='4' ><SetList>6deeec64-95f8-4e26-b5ad-e8770deaae5c</SetList><SetVar Name='Cmd'>Save</SetVar> <SetVar Name='ID'>4</SetVar><SetVar Name='urn:schemas-microsoft-com:office:office#SampleColumn'>mmmm</SetVar></Method>
</ows:Batch>

Cheers,
Nader Heshmat

Error: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

Hi all,

I just got this error while developing a web part to batch update a SharePoint list. The solution was adding one line of code that allows running the update command on a list without requiring a security validation. This should be place right before your update command.

Consider the code snippet below:

SPSite siteCol = new SPSite(YourSiteURL);
SPWeb web = siteCol.OpenWeb();
SPList list = web.Lists[YourListName];
web.AllowUnsafeUpdates = true;

{your list update code goes here}

Warning:
Setting this property to true opens security risks, potentially introducing cross-site scripting vulnerabilities.

p.s. You can also use web.Site.WebApplication.FormDigestSettings.Enabled = false before your update and set it to true after

Cheers,
Nader Heshmat

Post Navigation