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 = "<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; }
**********************************************
SPWeb oWebsite = SPContext.Current.Web; SPList oList = oWebsite.Lists["Announcements"]; SPQuery oQuery = new SPQuery(); oQuery.RowLimit = 10; 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);***********************************************
| Poor Performing Methods and Properties | Better Performing Alternatives |
|---|---|
| SPList.Items.Count | SPList.ItemCount |
| SPList.Items.XmlDataSchema | Create an SPQuery object to retrieve only the items you want. |
| SPList.Items.NumberOfFields | Create an SPQuery object (specifying the ViewFields) to retrieve only the items you want. |
| SPList.Items[System.Guid] | SPList.GetItemByUniqueId(System.Guid) |
| SPList.Items[System.Int32] | SPList.GetItemById(System.Int32) |
| SPList.Items.GetItemById(System.Int32) | SPList.GetItemById(System.Int32) |
| SPList.Items.ReorderItems(System.Boolean[],System.Int32[],System.Int32) | Perform a paged query by using SPQuery and reorder the items within each page. |
| SPList.Items.ListItemCollectionPosition | ContentIterator.ProcessListItems(SPList, ContentIterator.ItemProcessor, ContentIterator.ItemProcessorErrorCallout) (Microsoft SharePoint Server 2010 only) |
| SPList.Items.ListItemCollectionPosition | ContentIterator.ProcessListItems(SPList, ContentIterator.ItemProcessor, ContentIterator.ItemProcessorErrorCallout) (SharePoint Server 2010 only) |