locked
How to get Empty Folders From Document Library In SharePoint 2010 through CAML Query RRS feed

  • Question

  • Hi Guys,

    I have big libraray and there are some empty folders in it. I want to query on library and get all empty folders from CAML query.

    Please share how can we acheive this from CAML query or any other way?

    Please suggest.

    Thanks,

    Fahad Khan

    Sunday, August 4, 2013 1:25 PM

Answers

  • using (SPSite site = new SPSite("http://server/site"))
    {
      SPWeb web = site.RootWeb; 
      SPQuery query = new SPQuery();
      query.Query = @"
              <Where>
                <BeginsWith>
                  <FieldRef Name='ContentTypeId' />
                  <Value Type='ContentTypeId'>0x0120</Value>
                </BeginsWith>
              </Where>";
      query.ViewAttributes = "Scope='RecursiveAll'";
      SPList list = web.Lists[listId];
      SPListItemCollection items = list.GetItems(query);
      foreach(SPListItem i in items) {
          if (i.Folder.Files.Count == 0) {
            Console.WriteLine(i.Title + "is empty");
          }
      }
    }


    val it: unit=()


    • Edited by MSDN Student Sunday, August 4, 2013 9:35 PM
    • Marked as answer by star.wars Sunday, August 11, 2013 8:20 AM
    Sunday, August 4, 2013 9:34 PM

All replies

  • using (SPSite site = new SPSite("http://server/site"))
    {
      SPWeb web = site.RootWeb; 
      SPQuery query = new SPQuery();
      query.Query = @"
              <Where>
                <BeginsWith>
                  <FieldRef Name='ContentTypeId' />
                  <Value Type='ContentTypeId'>0x0120</Value>
                </BeginsWith>
              </Where>";
      query.ViewAttributes = "Scope='RecursiveAll'";
      SPList list = web.Lists[listId];
      SPListItemCollection items = list.GetItems(query);
      foreach(SPListItem i in items) {
          if (i.Folder.Files.Count == 0) {
            Console.WriteLine(i.Title + "is empty");
          }
      }
    }


    val it: unit=()


    • Edited by MSDN Student Sunday, August 4, 2013 9:35 PM
    • Marked as answer by star.wars Sunday, August 11, 2013 8:20 AM
    Sunday, August 4, 2013 9:34 PM
  • Sorry for the necroposting but im a begginer working on this same problem now.

     Just wondering, dont you should look for subfolders?

    I think the right conditional would be:

    	if ((i.Folder.Files.Count == 0) && (i.Folder.SubFolders.Count==0))   


    isn it?

    Anyway this is a pretty slow solution, i tried something like 

     q.Query = "<Where><And><And>" +
                                        "<Eq><FieldRef Name='FSObjType'/><Value Type='Lookup'>1</Value></Eq>" +
                                        "<Eq><FieldRef Name='ItemChildCount' /> <Value Type='Counter'>"+ 0 + "</Value></eq></And>" +
                                        "<Eq><FieldRef Name='FolderChildCount' /> <Value Type='Counter'>" + 0 + "</Value></eq>" +
                                        "</And></Where>";
                            q.ViewAttributes = "Scope=\"RecursiveAll\"";

    but cant get it to work.

     any alternative?

    Tuesday, September 8, 2015 3:51 PM