📜 ⬆️ ⬇️

Execute batch commands in SharePoint

The SharePoint object model provides many features that allow you to perform the same operation in different ways. Each of the methods has its advantages: one is easier to use, the other has better performance. One of such operations is the operation of batch insertion (editing, deleting) data in lists and libraries of SharePoint documents.

The most elementary way to add data to lists is to use the methods of the SPListItemCollection class. This method is useful if you need to add one or more records. If you need to process a large data array with optimal performance, we recommend using the ProcessBatchData method of the class SPWeb .

This method allows you to perform a batch of commands at once. For this purpose, a CAML request is sent to it as a parameter, which contains the necessary operations.

The following example uses the SPWeb class's ProcessBatchData method to add 100 entries to the list:
')
string listId = "C3715A5A-879B-4573-AED4-35E9DCD4E5F7" ;
string batchCommandFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:Batch OnError=\"Continue\">{0}</ows:Batch>" ;
string batchMethodFormat = "<Method ID=\"SaveItem_{0}\">" +
"<SetList Scope=\"Request\">{1}</SetList>" +
"<SetVar Name=\"Cmd\">Save</SetVar>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{2}</SetVar>" +
"</Method>" ;

StringBuilder methodsBuilder = new StringBuilder ();
for ( int i = 0; i < 20; i++)
{
methodsBuilder.AppendFormat(batchMethodFormat, i, listId, "Item title " + i);
}

string batchResult = web.ProcessBatchData( String .Format(batchCommandFormat, methodsBuilder));

* This source code was highlighted with Source Code Highlighter .

The main element of the CAML request is the <Method> element. The content of this element describes the very logic of the operation. In particular, using the <SetList> element, it is specified for which list the operation will be performed.

The <SetVar> element is used to set command parameters. So, with the help of it the type of the executed command and the field values ​​are set. At the same time, when setting the values ​​of the fields, the name of the fields must begin with the prefix urn: schemas-microsoft-com: office: office # (with some exceptions). In the case of inserting data for the ID field, you must specify the value New , which instructs SharePoint to generate the element ID automatically.

How much faster?

According to the measurements of Andreas Grabner and Eric Bartels, using the ProcessBatchData method gives an almost twofold advantage in execution speed. Moreover, such superiority is noticeable both on a large number of records (more than 200,000 records), and on relatively small amounts of data - 100 records.

However, when using ProcessBatchData, remember to use StringBuilder to generate CAML commands.

Examples of using

The following are examples of batch CAML requests for working with records in lists and SharePoint document libraries:
Related Links

CAML structure
WSS RPC methods

Source: https://habr.com/ru/post/79212/


All Articles