Andriy's profileGAS NotesPhotosBlogLists Tools Help

Blog


    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);

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://gaslivespace.spaces.live.com/blog/cns!13DAE509BE1BBE0B!257.trak
    Weblogs that reference this entry
    • None