tags:

views:

91

answers:

2

I have a List that stores items in a folder hierarchy.

I notice that SPFolder.Files.Count is always zero.

Is there a way to find out how many list items are there in a folder?

+1  A: 

I presume you are looking for direct children and not descendants (like items within a sub-folder).

Do you also want to include sub-folders in the count? In which case you can use: SPFolder.ItemCount.

If you just want only the direct child listItems which are not subfolders then you can do something like the following:

using (SPSite site = new SPSite(mySPSite))
{
    SPWeb web = site.OpenWeb();
    SPList list = web.Lists[myList];
    SPFolder folderInstance = list.RootFolder.SubFolders[folderUrl];

    SPQuery query = new SPQuery() ;
    query.Folder = folderInstance;

    SPListItemCollection items = list.GetItems(query) ;

    Console.WriteLine(items.Count);
}

I haven't tried it. You might have to add a where clause to eliminate folders, if the query is returning that.

If you want to include all list-items, even within subfolders, set the SPQuery.ViewAttributes field as query.ViewAttributes = "Scope=\"Recursive\"";

Moron
Yes, I need item count without sub-folders. Is it possible to get this without firing a query?
Vijay
Don't know. You might have some luck with SPFolder.Item.ListItems.
Moron
Nope. SPFolder.Items.ListItems didn't help. I used your solution as I could not find what I was looking for :) Thanks!
Vijay
A: 

From Microsoft

Using the SPList.ItemCount property is the recommended way to retrieve the number of items in a list. As a side effect of tuning this property for performance, however, the property can occasionally return unexpected results. For example, if you require the exact number of items, you should use ...

I wonder if the same applies to SPFolder.ItemCount ?

Ryan
SPFolder does not have ItemCount property defined!
Vijay