SharePoint: Difference between List.items.Count and List.ItemCount

The basic difference between List.items and List.ItemCount  is that list.items returns total no. of list items excluding folders while list.ItemsCount return total no. items in a list including folders.

Check SPList.ItemCount property

Check SPListItemCollection.Count property

When we compare List.items.Count vs List.ItemCount then one more major difference between them is that List.Items.Count shows only those items which are shared with the current user as item collection is security trimmed whereas List.ItemCount return all items and as it is not security-trimmed.

One more thing is that List.ItemCount is faster because it fetch the information from SPList property instead of building SPListItemCollection.

Deleting List Items throws “Specified argument was out of the range of valid values” Exception

One more thing is that List.ItemCount is faster because it fetch the information from SPList property instead of building SPListItemCollection.

And a good example of this is when you are trying to delete list items through loop using list.Count, you might get “Specified argument was out of the range of valid values” exception because you are deleting the list item from list item collection.

//Delete List items

for (int i = 0; i < list.Items.Count; i++)
{
list.Items[0].Delete();
}

To resolve the issue, use List.ItemCount which gives you the static item count value from list property.

for (int i = 0; i < list.ItemCount; i++)
{
list.Items[0].Delete();
}

Check difference between List.ItemCount and List.Items.Count in below screenshot where I executed a PowerShell Script to generate a report which lists list Item count for all list and libraries. In this picture you can clearly see the difference between them.List.ItemCount vs List.Items.Count

 

The following two tabs change content below.
Adnan Amin
Adnan is two time SharePoint MVP (Most Valuable Professional) with over 12 years of extensive experience with major expertise on SharePoint Server, Office 365, .Net Platform and Microsoft BI. He is currently working SharePoint Architect at FMT Consultants. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past nine years and worked on different intranet/intranet solutions for private & govt. sector majorly in Gulf region, which include OOB and customized solutions. He is a trainer, technology evangelist and also speaks in community forums.

2 Comments

  1. Pingback: Get SharePoint List Inventory using PowerShell

  2. We ɑre a group of ѵolunteers and opening
    a new scheme in our community. Your web site provided
    us wіth valuabⅼe information to work on. Yoᥙ havе
    done an impressive ϳob and our entire community will
    be thаnkful to you.

Leave a Reply

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