SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list that contains folders.

Using c# and object model, how can I see if the folder contains any items, or return a collection of all the items in that folder.

share|improve this question
Cant this above functionality done by using the services offered by sharepoint? – user15740 Mar 20 '13 at 5:14
up vote 18 down vote accepted

Try the following code to get the items based on the folder,

using(SPSite site = new SPSite("site url"))
  using(SPWeb web = site.OpenWeb())
    SPFolder folder = web.GetFolder("/Docs/folder1");
    if(folder.ItemCount > 0)
      SPList list = web.Lists.TryGetList("ListName");
      SPQuery query = new SPQuery();
      query.Folder = folder;
      SPListItemCollection listitem = list.GetItems(query);
share|improve this answer
Copycat :) But nothing really wrong because you tried to provided code example. I really hope that SPListItemCollection = list.GetItems(query); is typing error? And using TryGetList is meaningless because you don't check later if list != null. So if list doesn't exist your code will throw exception - same if you used web.Lists["ListName"]. – Vedran Rasol Sep 21 '11 at 13:17

You need to use SPQuery Class and define SPQuery.Folder Property

share|improve this answer
@soniiic when you down vote somebody it is polite to leave a comment – Vedran Rasol Sep 22 '11 at 21:32
foreach (SPFolder folder in list.Folders) {
    if (folder.ItemCount > 0) {
        foreach (SPFile file in folder.Files) {
            SPListItem item = file.Item;
            // Do stuff with your item here.
        foreach (SPFolder subfolder in folder.SubFolders) {
            // Do stuff with your folder here.
            // Think about perhaps using recursion if you want to list the items from this folder and beyond

Because we're using foreach, we don't really need to check if the itemcount is more than 0. But this is just an example to allow you to identify which folders have items and how you can access them

share|improve this answer
-1 - Right now I am working on one medium size library with ~15k documents in ~2k folders (2 levels deep) with size of few Gb. Running this code against it would be suicide. Using SPFile is also wrong if you don't want to change content of file. @soniiic please read Best Practices: Common Coding Issues When Using the SharePoint Object Model – Vedran Rasol Sep 21 '11 at 11:16
No one said that this was a large list. The solution works, and I was able to provide a code example that he could use. You could have simply written a comment rather than down voting my answer. Shantha's code didn't limit the result set in any way when using the SPQuery so both our code would run at the same speed, except with mine you'd have some scope to work with so you knew which folder you were at when. At least I tried to help. Next time I won't, in fear of someone coming along and saying that it wouldn't work with 15k documents... – soniiic Sep 21 '11 at 15:27
It doesn't matter if you have 15k or just one item, bad coding practices are always bad. Instead of whining you should learn to accept criticism. And after reading your comment I don't have any doubts about my down vote. – Vedran Rasol Sep 21 '11 at 19:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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