November 06
Paginate the SharePoint list in increments of no more than 2,000 items
Working With Large List in MOSS 2007 http://technet.microsoft.com/en-us/library/cc262813.aspx
============================================================
Example 1 (Apples for MOSS 2010):
SPQuery query = new SPQuery();
SPListItemCollection spListItems ;
string lastItemIdOnPage = null; // Page position.
int itemCount = 2000
while (itemCount == 2000)
{
// Include only the fields you will use.
query.ViewFields = "<FieldRef Name=\"ID\"/><FieldRef Name=\"ContentTypeId\"/>";
query.RowLimit = 2000; // Only select the top 2000.
// Include items in a subfolder (if necessary).
query.ViewAttributes = "Scope=\"Recursive\"";
StringBuilder sb = new StringBuilder();
// To make the query order by ID and stop scanning the table, specify the OrderBy override attribute.
sb.Append("<OrderBy Override=\"TRUE\"><FieldRef Name=\"ID\"/></OrderBy>");
//.. Append more text as necessary ..
query.Query = sb.ToString();
// Get 2,000 more items.
SPListItemCollectionPosition pos = new SPListItemCollectionPosition(lastItemIdOnPage);
query.ListItemCollectionPosition = pos; //Page info.
spListItems = spList.GetItems(query);
lastItemIdOnPage = spListItems.ListItemCollectionPosition.PagingInfo;
// Code to enumerate the spListItems.
// If itemCount <2000, finish the enumeration.
itemCount = spListItems.Count;
}
============================================================
Example 2:
SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["Announcements"];
SPQuery oQuery = new SPQuery();
oQuery.RowLimit = 10;
oQuery.Query = "<OrderBy Override=\"TRUE\">" +
"<FieldRef Name=\"FileLeafRef\" /></OrderBy>";
int intIndex = 1;
do
{
Response.Write("<BR>Page: " + intIndex + "<BR>");
SPListItemCollection collListItems = oList.GetItems(oQuery);
foreach (SPListItem oListItem in collListItems)
{
Response.Write(SPEncode.HtmlEncode(oListItem["Title"].ToString()) +
"<BR>");
}
oQuery.ListItemCollectionPosition =
collListItems.ListItemCollectionPosition;
intIndex++;
} while (oQuery.ListItemCollectionPosition != null);