Pages

February 18, 2014

Comparing Ways to Counting Items in SPList


This is a quick comparison between several ways to count the number of items in SPList:

  • SPList.Items
  • SPList.GetItems(SPQuery).Count
  • SPList.ItemCount

First, keep in mind that SPList.Items.Count is a special case of the SPList.GetItems(SPQuery).Count.
Here is how it constructed behind the scenes inside Microsoft.SharePoint.dll:

public SPListItemCollection Items
        {
            [ClientCallableExceptionConstraint(FixedId="c", ErrorType=typeof(SPQueryThrottledException), Condition="There is a throttle failure.", ErrorCode=-2147024860)]
            get
            {
                SPQuery query = new SPQuery {
                    ViewAttributes = "Scope=\"Recursive\""
                };
                return this.GetItems(query);
            }

        }


So here is technical & usage comparison:




SPList.Items.Count


SPList.GetItems(SPQuery).Count

SPList.ItemCount
In doc library
Includes all Files in all Folders
Except: folders and checked-out files

Depending on query scope. Can include all folders + all Files in all Folders
Except: checked-out files
Includes all files, folders and subfolders + Checked out files
In list

All list items
All list items
All list items
Based on View

Yes (Default All Items view)
Yes (Default All Items view)
No
User Security Trimming 

Yes
Yes
No
Retrieval Mechanism

Based on list query
Based on list query

Based on List Metadata properties
Depth Scopes



Recursive: Includes all Files in all Folders but not Folders themselves


4 supported scopes:
·     Default: Includes all Files and Folders in current Folder
·     Recursive: Includes all Files in all Folders but not Folders themselves
·     RecursiveAll: Includes all folders and all Files in all Folders
·     FilesOnly: Includes all Files in all Folders in current Folder

N/A
Resource Throttling

Yes
Yes. Can be overridden
No
Accuracy

Not Guaranteed
Guaranteed
Not Guaranteed
Performance

Low
Good
Very High
When to use



Not recommended
In planning & displaying items of small to medium list/library
In planning; To asses size of very large list/library


I hope you find it useful & handy, cheers!