Why can't I access the items in a List even if the SPList.ItemCount is > 0?
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 diff开发者_C百科erent? 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.
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.
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?
精彩评论