2

If I need to store say 10,000 items in a list, but want to call them sequentially 2000 items at a time, what is the best method for doing that? I assume it's similar to a paged call, but want to execute this the right way.

This is in SharePoint 2007

3

You can use the built-in SPQuery paging mechanism

SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["Announcements"];

SPQuery oQuery = new SPQuery();
oQuery.RowLimit = 2000;
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);
  • You sir, are a gentleman and a scholar. – Wesley Apr 25 '12 at 17:47
  • 5
    Nope, I just know how to Google :) – Rob Windsor Apr 25 '12 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.