I have working code that is to iterate lots of folders in our (rather large) document archive, but one folder (which happens to have a very large amount of subfolders) does not allow me to iterate it.

Supposing I do this... SPFolder folder = web.Folders["Documents"]; foreach (SPFolder f in folder.SubFolders) { // THIS CODE NEVER GETS CALLED?!?! // but I see the subfolders via the front end, // and the ItemCount for the folder says 8000 }

If I look at the object in the debugger, I see it has: folder.ItemCount = 8967 folder.SubFolders.Count = 0 folder.Files.Count = 0 Any idea how this could be?? ...and how I could iterate through the subfolders? If the "ItemCount" for the folder isn't based on subfolders or files - what is it counting? I'm wondering if this could be the wonky "SharePoint performance throttling 5000-list-item-limit" thing that's keeping me from seeing the subfolders in the SubFolders collection, but I tried upping the limit in Central Admin to no avail.

If I simply navigate to the folder via the web site, then I see all of the subfolders.

Thanks, CList

  • There are several approaches to reading items in a list that is subject to the threshold. You don't explain what you are trying to accomplish, so it is hard to say which is best. Feb 21, 2014 at 18:57
  • I'm trying to iterate through the subfolders of this folder. SPFolder folder = web.Folders["Documents"]; Assert(folder.ItemCount > 5000); // TRUE! Assert(folder.SubFolders.Count > 1); // FALSE! foreach (SPFolder f in folder) { // THIS CODE NEVER GETS CALLED?!?! // but I see the subfolders via the front end, and the ItemCount for the folder says 8000 }
    – C.List
    Feb 21, 2014 at 19:47

3 Answers 3


Threshold shouldn't be the issue here. You have changed the value in CA and you don't seem to be getting any exceptions. SPFolder is not returning everything recursively, and just dumps a lot of unspecified SPItems. I would recommend using ContentIterator with ProcessFilesInFolder taking an SPFolder http://www.askaboutasp.net/_-_105_What_is_the_best_way_to_iterate_through_a_SharePoint_2010_Folder_Recur.aspx



The least I can say is that the line

foreach (SPFolder f in folder)

should be

foreach (SPFolder f in folder.SubFolders)

Although I am debugging in my head right now, if this is a large list (and it is) the code will certainly fail in real life.

You can use the ContentIterator class, but as it was said before, we don;t know what you are trying to achieve.

  • Yes - correct - sorry for the typo - I've updated my question!
    – C.List
    Feb 24, 2014 at 15:47

That did the trick... note that I needed to iterate folders and not files, so that I could deleted any folders that happen to be empty. I therefore had to use the Item Iterator rather than the file iterator like so: SPList list = RootWeb.Lists[folder.ParentListId]; Microsoft.Office.Server.Utilities.ContentIterator ci = new Microsoft.Office.Server.Utilities.ContentIterator(); ci.ProcessItemsInFolder(list, folder, true, true, new Microsoft.Office.Server.Utilities.ContentIterator.ItemProcessor((SPListItem i) => { if (i.Folder != null) { CheckFolder(i.Folder); } }), new Microsoft.Office.Server.Utilities.ContentIterator.ItemProcessorErrorCallout((SPListItem i, Exception Ex) => { //Define the action I need to do if an error occur BatchLogProxy.WriteError(Ex); return true; }) );

Thanks for pointing me in the right direction. A side effect of using these iterators is that my iterations seem to be faster.

Cheers, CList

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.