14

A list on my site gives 2 when I call myList.ItemCount. But myList.Items.Count gives me zero and I can't access the items stored in the list. AllItems.aspx show me the two missing items.

Any ideas?

7

I remember that @furuknap once pointed out that ItemCount and Items.Count are different in the way they treat SPFolders (inclusive and exclusive). Also, ItemCount is faster because it gets a static SPList property instead of building the SPListItemCollection object before counting its members.

  • This is indeed the difference in my case (SP 2013): spList.ItemCount = spList.Items.Count + spList.Folders.Count in various cases. – R. Schreurs Jun 14 '18 at 9:10
3

I looked closely with Reflector and found this, SPList.Items returns the list items by running a query with Scope=Recursive ViewAttribute. I don't have enough time to do some research on the topic but simply running a query with no attributes gave me my items.

1

SPList.ItemCount is the efficient and recommended way to retrieve items count in a list.

However, It should be noted that this property is fine tuned for performance which may cause the side effect of returning unexpected count. So, if you need the exact count, you should use the poorer performing SPList.Items.Count or SPList.GetItems(SPQuery query).Count

1

Permissions might be the issue. I ran into the same thing, and it turned out that I forgot to make the list accessible.

Why can't I access the items in a List even if the SPList.ItemCount is > 0? - StackOverflow

0

Check whether the user u logged in may not assigned in ur site collection..... i too meet the same problem.... now cleared.....

add the u user what u logged in to the site collection and then check it....

  • Welcome to SharePoint StackExchange. Although your answer may answer the question, links to blog posts often break, where as we won't be going anywhere for a long while. It's always a good idea to extract the useful parts for research if not all of it incase that happens (If it's not too long!) Please read the FAQ[1] for more information [1]:sharepoint.stackexchange.com/faq – Hugh Wood Dec 2 '12 at 3:18
0

SPList.ItemCount, which is count of all items in the list. SPList.Items.Count, which is count of completed tasks/Check In pages. (Means it avoid the incompleted items)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy