Working with Larger Folders and Lists

When you are working with folders and lists:

  • Do not use SPList.Items.

    SPList.Items selects all items from all subfolders, including all fields in the list. Use the following alternatives for each use case.

    • Adding an item

      Instead of calling SPList.Items.Add, use SPList.AddItem.

    • Retrieving all items in a list

      Instead of using SPList.Items, use SPList.GetItems(SPQuery query) . Apply filters, if appropriate, and specify only the fields you need to make the query more efficient. If the list contains more than 2,000 items, paginate the list in increments of no more than 2,000 items. The following code example shows how to paginate a large list.

      Good Coding Practice Retrieving items with SPList.GetItems

      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 = "";   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(""); //.. 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; }

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s