Batch Update - SharePoint

Hi Guys, Today i would like to explain you about the batch update process in SharePoint. Batch (Group of items), the meaning is to update a large number of items to the SharePoint list by passing the values as XML elements. Consider if u want to update the list with more than 100 items. In this case if u use foreach it will be very expensive and it suck ur time. Moreover if u use SPListItem.Update() it takes 2.8secs for updating a single item. If u r using ProcessBatchUpdate it takes 30 secs to update to a maximum of 200 - 300 items based on ur requirement. So my suggestion is to use ProcessBatchUpdate for Adding, Updating and Deleting a large number of items from the SharePoint List.

Lets see how to perform this command through the SharePoint object model

Consider that u r adding items in the list with Title field as a running numbers. This is for example ah

First of all u should format the XML file for Inserting, Updating or Deleting the items in list

StringBuilder methodBuilder = new StringBuilder();
string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                "<ows:Batch OnError=\"Return\">{0}</ows:Batch>";


// For Updating
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#Job_x0020_No\">{3}</SetVar>" +
                                   "</Method>";

For update command pass the ItemID for ID parameter and the values.

// For Inserting
string methodFormat = "<Method ID=\"{0}\">" +
                                   "<SetList>{1}</SetList>" +
                                   "<SetVar Name=\"Cmd\">Save</SetVar>" +
                                   "<SetVar Name=\"ID\">New</SetVar>" +
                                   "<SetVar Name=\"urn:schemas-microsoft-com:office:office#Job_x0020_No\">{2}</SetVar>" +
                                   "</Method>";

for (int i = 0; i < 10000; i++)
{
    methodBuilder.AppendFormat(methodFormat, i, listGuid, "1");
}


If u observe the code we are passing the MethodID as running numbers, listguid, values

// For Deleting
string methodFormat = "<Method ID=\"{0}\">" +
                                   "<SetList>{1}</SetList>" +
                                   "<SetVar Name=\"Cmd\">Delete</SetVar>" +
                                   "<SetVar Name=\"ID\">{2}</SetVar>" +
                                   "</Method>";

For delete command get the item collection to delete and pass the ItemID to delete.

Note: Please pass the Fields internal name in the XML.

batch = string.Format(batchFormat, methodBuilder.ToString());

The above method will perform the operation and will return XML value as string. It is not necessary. Just capture that value in a string.

I hope u understand the concept of ProcessBatchUpdate in SharePoint. Just give a try and later u will become impressed. If there is any query don't forget to mail me. Catch me at sudharsan_1985@live.in, sudharsan.zylog@gmail.com

 Cheers...



Comments

Popular posts from this blog

PowerShell to update SharePoint Online User Profile property from Azure AD

SPFx - Office UI Fabric react DetailsList & PropertyFieldCodeEditor to show the CSV data

Using React Context API in SPFx