views:

54

answers:

2

I have the same code running against two different SharePoint installations, to iterate through the items in a List and display the data. Against the first SharePoint installation,

SPList.ItemCount == SPList.Items.Count == 4.

Against the second SharePoint installation, SPList.ItemCount == 4 while SPList.Items.Count == 0, and the foreach loop iterating through the List's Items doesn't work (because there aren't any Items). (Needless to say, the lists on both the SP installations have the same items).

The code I'm using can be summarized thus:

...

SPList list = web.Lists["list"];
SPListItemCollection itemCollection = list.Items;

Console.WriteLine("list.ItemCount: {0}", list.ItemCount); //prints "list.ItemCount: 4"
Console.WriteLine("list.Items.Count: {0}", list.Items.Count); //prints "list.Items.Count: 0"

foreach (SPListItem item in itemCollection)
{
    //nothing executed here, since itemCollection is empty,
    //even though list.ItemCount is 4.
}

...

What would cause the SPList.ItemCount and the SPList.Items.Count to be different? And, more importantly, why can't I iterate over the items in the list, even when SPList.ItemCount > 0 ?

A duplicate of this question appears on SharePoint Overflow, but it doesn't fully explain the solution.

+1  A: 

The value of the ItemCount property includes folders within a document library, as well as files within subfolders. The value of the Count property of the SPListCollection class does not include folders.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.itemcount.aspx

My theory is that it has something to do with folders, I know you said they are the same install. Are you certain that is the case. Have you physically checked the file listing via the UI or some other means?

Aaron
Yep, I have. The items are in the list, as accessed via allitems.aspx. There are no folders, only items.
Ryan Shripat
+1  A: 

Item-level permissions may be keeping the list.Items from exposing every item to the user-context under consideration, and therefore, the count off the property.

Jesse C. Slicer
In this case, it wasn't Item-level permissions, but the List permissions itself - I thought I had enabled anonymous access on the list, but it turns out I didn't. Your answer put me in the right direction, thanks ! Strange that the Count and ItemCount properties return different values though.
Ryan Shripat
Glad to have helped! One thing I just learned recently if you run into performance issues is that `SPList.Items.GetItemById()` is MUCH slower than `SPList.GetItemById()`. Reason being that calling the `Items` property on `SPList` *immediately* fetches all items on the list, which, in some cases you don't want.
Jesse C. Slicer