Batch Updating List Items in Windows SharePoint Services 3.0
Summary: Learn how to update multiple items in a list in Windows SharePoint Services 3.0 by constructing and executing a batch of commands.
Applies to: 2007 Microsoft Office system, Windows SharePoint Services 3.0
In Windows SharePoint Services 3.0, sometimes you must update multiple items in a list. Creating a foreach loop to iterate through a list item collection and performing an Update on each list item can be very intensive. An alternative approach is to construct a Collaborative Application Markup Language (CAML) string that contains a batch of commands to perform the updates and execute the commands by using the ProcessBatchData command of an SPWeb object. This Microsoft Office Visual How To demonstrates how to construct and execute a batch of commands to update text and date fields for all items in a list.
This example uses a document library named Processed Documents that contains a Processed yes/no field and Processed Date date field. You use a query to obtain a collection of items that have not been processed, as identified by their Processed value. You create a CAML statement to update the Processed and Processed Date fields for all documents in this library.
After setting up the variables, you instantiate an SPList object for the Processed Documents document library by first instantiating an SPSite object and then an SPWeb object that contains the library. After getting the SPList object, you construct and execute a query for all items in the library that do not have a Processed value of 1, that is, all items that have not been processed. The query returns an SPListItemCollection object of items that meet this criterion.
The next step is to build the CAML batch command that is used to perform the update. Each command includes a Method element with SetVar subelements. The SetVar elements define the type of command, the GUID of the list, the ID of the item, and the fields to update. The fields are defined in the format <schema>#<internal_field_name>.
As the update command for each item is created, it is appended to a StringBuilder object. After all the commands are created, they are placed in an ows:Batch element. This element contains an OnError attribute that defines what happens if an exception occurs while the commands are executed. In this example, execution stops on the first exception. The full XML string that defines the batch of commands is executed using the ProcessBatchCommand command of the SPWeb object. It returns a string that contains the results of each command.
The query used in this example is intended to update a relatively small number of list items. As the number of items in the list, and therefore, the query, grow, you must take specific precautions to ensure optimal performance. Primarily, you should set the RowLimit property of the SPQuery object. This property defines the number of items returned per page. Because the returned page of data is in-memory, the value should be tuned based on memory limits, but should be less than 2000.
The RowLimit property is used together with the ListItemCollectionProperty and a loop to page through the results. In the following code example, which is based on the example in this Visual How To, the RowLimit of the query is set to return 100 items per page of data, and a do/while loop is used to page through all of the items.
To further optimize this example, you can place an index on the Processed column in the Processed Documents library.
Processing commands in a batch can be very helpful for developers. It is one of the faster methods for updating, and you can use batch commands for inserting and deleting items. Although this example shows how to do a batch update through the object model, you can also do batch processing through several Web services, such as the UpdateListItems method on the Lists Web service, and the UpdateDwsData method on the Dws Web service.
To correctly format the date, use the SPUtility.CreateISO8601DateTimeFromSystemDateTime method.
Length: 07:02 | Size: 5.24 MB | Type: WMV