While performing certain task in a console application, i am unable to separate normal folder and hidden ones such as "Forms","FormServerTemplates" etc. I am using SPFolder in the obejct model. I have tried both FolderObject.property.count=21 and FolderObject.item==null, but none of these seems to filter the hidden folders.

Scenario : To copy all folders and files inside root folder of document library. Excluding hidden folders such as "Forms", "FormServerTemplates" etc.

static void traverseFolder(SPFolder fold, string path)


                if (!(fold.Item==null))

                    var Listdir = path + @"\" + fold.Name.ToString();
                    string listpath = Listdir.ToString();
                    if (!Directory.Exists(Listdir))

                    foreach (SPFolder subFold in fold.SubFolders)
                        if (!(subFold.Item==null))
                            traverseFolder(subFold, listpath);

                    foreach (SPFile file in fold.Files)
                        byte[] binFile = file.OpenBinary();

                        System.IO.FileStream fstream =

                        System.IO.File.Create(listpath + "\\" + file.Name);

                        fstream.Write(binFile, 0, binFile.Length);


                }      //Console.WriteLine(item.File.Name);

            catch (Exception ex)
                EventLog myException = new EventLog("");
                myException.Source = "SiteCol File Copy Exception";
                myException.WriteEntry("Error in copying " + ex.Message,EventLogEntryType.Error);


Is there any other way to separate?


  • I am just curious, SPFolder.Item==null condition usually filters out forms and related folders, it definitely filters out "Forms" because I used it several times, for which folder the condition is failing?
    – Diptarag
    Commented Nov 28, 2012 at 7:46
  • @Diptarag: If i use the condition if (!(fold.Item==null)) then it will not go inside the if condition. Therefore no folder or item within the folder would be traversed. Commented Nov 28, 2012 at 8:45
  • Let me get the scenario you want to traverse the "Forms" (and similar folder) and also normal user created folders but logic for handling those would be different? Why cant you use if-else? If you state your requirement and sample code that will be really helpful.
    – Diptarag
    Commented Nov 28, 2012 at 8:50
  • @Diptarag: I have added the scenario as well as the code. I tried if-else, but that didnt work. Commented Nov 28, 2012 at 8:58

1 Answer 1


Option 1

Use SPFolder.Item Property to exclude system folders (those folders that does not belong to a document library):

 public static void ProcessFolder(SPFolder folder,Action<SPFolder> action)
        foreach (var subfolder in folder.SubFolders.Cast<SPFolder>().Where(f => f.Item != null))


using (var site = new SPSite(siteUrl))
     using (var web = site.OpenWeb())
         SPList list = web.Lists.TryGetList("Documents");
         ProcessFolder(list.RootFolder,(f) => Console.WriteLine(f.Name));

Option 2

Using CAML query:

  static IEnumerable<SPFolder> GetFolders(SPList list, bool recursive)
        var query = new SPQuery
                Query = "<Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Integer\">1</Value></Eq></Where>" 
            query.ViewAttributes = "Scope=\"RecursiveAll\"";
        var items = list.GetItems(query);
        return items.Cast<SPListItem>().Select(i => i.Folder);


var list = web.Lists.TryGetList("Assets");
var folders = GetFolders(list,true);
foreach (var f in folders)

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.