Monday, January 24, 2011

SPWeb.ProcessBatchData method to improve SharePoint list operations performance

The common way to add or update multiple items in SharePoint list is SPListItem.Update. You can an existing item or add a new item using SplistItemCollection .Add(). 

Sample Code Snippet:

for (int itemCount = 0; itemCount < 100;s itemCount++)
SPListItem newItem = listItemCollection.Add();

Using Batch Update:
ProcessBatchData method takes XML format as input and it will directly communicate with SharePoint content database.

StringBuilder query = new StringBuilder();
query.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
for (int itemCount = 0; itemCount < 100; itemCount++)
query.AppendFormat("<Method ID=\"{0}\">" +
"<SetList>{1}</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=\"{3}Title\">{2}</SetVar>" +
"</Method>", i, listGuid, someValue, "urn:schemas-microsoft-com:office:office#");

If you have to update a larger number of items its highly recommended to not use the Update method on every item. Instead – use the batch update function ProcessBatchData provided by SPWeb.

Reference Links:

No comments:

Post a Comment