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}”;
// For Updating
string methodFormat = “” +
                                   “{1}” +
                                   “Save” +
                                   “{2}” +
                                   “{3}” +
                                   “”;
For update command pass the ItemID for ID parameter and the values.
// For Inserting
string methodFormat = “” +
                                   “{1}” +
                                   “Save” +
                                   “New” +
                                   “{2}” +
                                   “”;
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 = “” +
                                   “{1}” +
                                   “Delete” +
                                   “{2}” +
                                   “”;
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…

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