Thursday, May 3, 2012

SharePoint Tips: List.ItemCount vs List.Items.Count

If you need to know the total items in the list, how do you write code? The usual way to write code is shown below:
var itemCount = list.Items.Count;
Code Snippet 1: Usual (not suggested) way to get items count
However this will fetch all the records from database and apply the count in memory.

SharePoint object model provides an easiest way to find the items count without fetching all records and you can use the following code snippet to do so:
var itemCount = list.ItemCount;
Code Snippet 2: Suggested way to get items count

2 comments:

  1. And you should be aware of that the suggested way is not always up-to-date, and therefore totaly out of question for me...

    ReplyDelete
  2. You could see the tracing in the by turn on the Dashboard Developer to see how the SharePoint execute the query from database if you get the item count by list.Items.Count. That's no best practice.

    ReplyDelete