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.