SPList.ItemCount property does not return correct count

The SPList.ItemCount property is the recommended way to retrieve the number of items in a list. As a side effect of tuning this property for performance, however, the property can occasionally return unexpected results. If the precise number is required, you should use the poorer performing GetItems(SPQuery query).
Using SPQuery to get the item count is the best way to get the item count… just like this eg
query.ViewFields = "";
query.RowLimit = 2000; // Only select the top 2000.
// Include items in subfolder (if necessary).
query.ViewAttributes = "Scope=\"Recursive\"";
StringBuilder sb = new StringBuilder();
// To make it order by ID and stop scanning the table, specify the OrderBy override attribute.
//.. 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, we finish the enumeration. itemCount = spListItems.Count;

Leave a Reply

Your email address will not be published. Required fields are marked *