Saturday, May 07, 2011

Retrieving SharePoint List data in blocks

 

To reduce the impact on the sever, you could use SPQuery object’s RowLimit property to get data from SharePoint list in blocks.

The below code shows how one could get data in block of 10s-

SPList list = web.Lists["News"];
SPQuery query = new SPQuery();
// Define the query
query.Query = "<Where><Contains><FieldRef Name=\"Title\" />
<Value Type=\"Text\">San</Value></Contains></Where>";


// Define the maximum number of results for each page (like a SELECT TOP)
query.RowLimit = 5;


do {
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items) {
}
// Set current position to make SPQuery able to set the start item of the next page
query.ListItemCollectionPosition = items.ListItemCollectionPosition;
} while (query.ListItemCollectionPosition != null);

The ListItemCollectionPosition property is of type SPListItemCollectionPosition. It offers a PagingInfo property of type String, which contains the following data:
Paged=TRUE&p_ID=10


The _ID is the unique identifier of the last item retrieved so that SharePoint can know the starting position of the next page.

0 comments: