SPList ItemCount on a > 100.000 items list

SPList ItemCount on a > 100.000 items list header image

Having a list containing more than 100.000 items provided us with some challenges, apparently it is not possible to retrieve the ItemCount based on certain criteria using the object model.

The problem is that we actually had to retrieve those items. So a colleague came up with a rather dirty but very useful fix for that. It’s a small piece of code that makes a View based on a desired query, and sort this on the Author (all the items are created by the same account). By setting the Collapse property on true SharePoint renders a HTML Header containing the total amount of items. By actually reading that piece of HTML he found a quick and dirty way to retrieve those totals.

Below you can find the code, and we were wondering if any of you out there would know another way without using the Search or talking directly to the Content Database.

///
/// This function will return the count of the items found by the query, using the RenderAsHTML() method of the SPView object
/// IMPORTANT: It assumes that all Items are created using the same account (Author), if this is not the case,
/// please find or provide an other property that is equal for all ListItems
/// NOTE: This function has no error-handling inside.
///
/// SPList object that holds the items to count
/// CAML query-string
/// -1 on error, otherwise number of items found

private static int GetItemCount(SPList list,string query) {
  return GetItemCount(list, query,"Author");
  }
private static int GetItemCount(SPList list,string query,string GroupByProperty)     {
  //Since files are added by the system, author will be the same for all
  //adding GroupBy and setting the Collapse to true, the view will be rendered collapsed and show only the itemcount
  query = String.Format("{1}",GroupByProperty, query);
  //create temp view
  list.ParentWeb.AllowUnsafeUpdates =true;
  string TempViewName ="TempViewForItemCount";
Loading comments…