locked
How to fetch folders and subfolders from sharepoint document library RRS feed

  • Question

  •  I have document library with name . Under "Documents" there are some folders.Under some folders there are some subfolders.

    I need to fetch the folders in to dropdown list. IF I select some folder in dropdownlist,I need to fetch subfolders of that folder to some other dropdownlist.
    How to acheive this
    Friday, February 17, 2012 7:33 AM

Answers

  • To get list of folders in the first dropdown:

    using(SPSite oSite = new SPSite("Site_URL"))
                
    {
                    SPWeb oWeb = oSite.OpenWeb();
                    SPList oList = oWeb.Lists["Documents"];
    
                    SPFolderCollection oFolders = oList.RootFolder.SubFolders;
    
                    foreach (SPFolder oFolder in oFolders)
                    {
                        if (!oFolder.Name.Equals("Forms"))
                        {
                            // use oFolder.Name to populate list                        
                        }
                    }
                }

    When a folder name is selected from drop down, using following code:

    SPList oList = oWeb.Lists["Documents"];
    
                    SPFolder selFolder = oList.RootFolder.SubFolders[oList.Title + "/" + //Selected Folder Name];
                    SPFolderCollection selSubFolders = selFolder.SubFolders;
    
                    foreach (SPFolder selSubFolder in selSubFolders)
                    {
                       // use selSubFolder.Name property to populate second list
                    }


    http://farhanfaiz.wordpress.com

    • Marked as answer by Qiao Wei Thursday, February 23, 2012 10:51 AM
    Friday, February 17, 2012 10:27 AM
  • Here is the code of a web part that do what you want for the Shared Documents library :

    DropDownList ddl1;
            DropDownList ddl2;
            protected override void CreateChildControls()
            {
                ddl1 = new DropDownList();
                ddl2 = new DropDownList();
                ddl1.ID = "ddl1";
                ddl2.ID = "ddl2";
                
                ddl1.SelectedIndexChanged += new EventHandler(ddl1_SelectedIndexChanged);
                ddl1.AutoPostBack = true;
                this.Controls.Add(ddl1);
                this.Controls.Add(ddl2);
            }
    
            protected override void OnInit(EventArgs e)
            {
                
                base.OnInit(e);
                
                
            }
            protected override void OnLoad(EventArgs e)
            {
                EnsureChildControls();
                base.OnLoad(e);
                if (!Page.IsPostBack)
                {
                    SPList list = SPContext.Current.Web.Lists.TryGetList("Shared documents");
                    ddl1.DataSource = list.RootFolder.SubFolders;
                    ddl1.DataBind();
                }
                
                
            }
    
            void ddl1_SelectedIndexChanged(object sender, EventArgs e)
            {
                SPList list = SPContext.Current.Web.Lists.TryGetList("Shared documents");
                ddl2.DataSource = list.RootFolder.SubFolders[ddl1.SelectedIndex].SubFolders;
                ddl2.DataBind();
            }

    Regards.

    Batiste

    • Proposed as answer by Geetanjali Arora Friday, February 17, 2012 11:24 AM
    • Marked as answer by Qiao Wei Thursday, February 23, 2012 10:51 AM
    Friday, February 17, 2012 11:20 AM

All replies

  • One way is to get folders from document library using SPQuery 

    <Query><Where><Eq><FieldRef Name="ContentType" /><Value Type="Computed">Folder</Value></Eq></Where></Query>

    or use SPList.Folders property to get folders

    Use the SPFolder.ParentFolder property to get the folders at first level.

    Now, you can use SPFolder.SubFolders propert to get all the sebfolder from a folder or

    again using SPQuery by specifying SPQuery.Folder property


    http://farhanfaiz.wordpress.com

    Friday, February 17, 2012 8:09 AM
  • You can fetch folders by using SPQuery.

    Use this where clause, it will filter the results to folders only:

    query.Query = "<Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Number\">1</Value></Eq></Where>";

    You can traverse through folders by using the Folder property of SPQuery.

    e.g.

    SPQuery query = new SPQuery();
    query.Folder = myList.RootFolder; //change this to target folder, when you travers, by specifying list item, then using its Folder property (myItem.Folder)query.ViewAttributes = "Scope=\"RecursiveAll\"";
    query.Query = "<Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Number\">1</Value></Eq></Where>";
    SPListItemCollection liColl = myList.GetItems(query);

    Friday, February 17, 2012 8:10 AM
  • Hi,

      You can use

    SPList.RootFolder.SubFolders

    To get the folders inside your list, then for each folder you can do the same by binding  your dropdown to the SubFolders property of the folder.

    Regards.

    Batiste

    Friday, February 17, 2012 8:29 AM
  • Can I have the piece of code?
    Friday, February 17, 2012 9:12 AM
  • I used the following to reccursively get files in Shared Documents:

     SPDocumentLibrary lib = site.RootWeb.Lists["Shared Documents"] as SPDocumentLibrary;
     ProcessSPFolder(lib.RootFolder);
     
     static void ProcessSPFolder(SPFolder folder)
     {
        //logic

        foreach (SPFile file in folder.Files)
        {
           //logic
        }

        foreach (SPFolder f in folder.SubFolders)
        {
          ProcessSPFolder(f);
        }
     }

    Friday, February 17, 2012 9:21 AM
  • Hi,

      You can use

    SPList.RootFolder.SubFolders

    To get the folders inside your list, then for each folder you can do the same by binding  your dropdown to the SubFolders property of the folder.

    Regards.

    Batiste

    Bastiste is absolutely right

    SharePoint 2010 PowerShell

    Friday, February 17, 2012 9:45 AM
  • To get list of folders in the first dropdown:

    using(SPSite oSite = new SPSite("Site_URL"))
                
    {
                    SPWeb oWeb = oSite.OpenWeb();
                    SPList oList = oWeb.Lists["Documents"];
    
                    SPFolderCollection oFolders = oList.RootFolder.SubFolders;
    
                    foreach (SPFolder oFolder in oFolders)
                    {
                        if (!oFolder.Name.Equals("Forms"))
                        {
                            // use oFolder.Name to populate list                        
                        }
                    }
                }

    When a folder name is selected from drop down, using following code:

    SPList oList = oWeb.Lists["Documents"];
    
                    SPFolder selFolder = oList.RootFolder.SubFolders[oList.Title + "/" + //Selected Folder Name];
                    SPFolderCollection selSubFolders = selFolder.SubFolders;
    
                    foreach (SPFolder selSubFolder in selSubFolders)
                    {
                       // use selSubFolder.Name property to populate second list
                    }


    http://farhanfaiz.wordpress.com

    • Marked as answer by Qiao Wei Thursday, February 23, 2012 10:51 AM
    Friday, February 17, 2012 10:27 AM
  • Here is the code of a web part that do what you want for the Shared Documents library :

    DropDownList ddl1;
            DropDownList ddl2;
            protected override void CreateChildControls()
            {
                ddl1 = new DropDownList();
                ddl2 = new DropDownList();
                ddl1.ID = "ddl1";
                ddl2.ID = "ddl2";
                
                ddl1.SelectedIndexChanged += new EventHandler(ddl1_SelectedIndexChanged);
                ddl1.AutoPostBack = true;
                this.Controls.Add(ddl1);
                this.Controls.Add(ddl2);
            }
    
            protected override void OnInit(EventArgs e)
            {
                
                base.OnInit(e);
                
                
            }
            protected override void OnLoad(EventArgs e)
            {
                EnsureChildControls();
                base.OnLoad(e);
                if (!Page.IsPostBack)
                {
                    SPList list = SPContext.Current.Web.Lists.TryGetList("Shared documents");
                    ddl1.DataSource = list.RootFolder.SubFolders;
                    ddl1.DataBind();
                }
                
                
            }
    
            void ddl1_SelectedIndexChanged(object sender, EventArgs e)
            {
                SPList list = SPContext.Current.Web.Lists.TryGetList("Shared documents");
                ddl2.DataSource = list.RootFolder.SubFolders[ddl1.SelectedIndex].SubFolders;
                ddl2.DataBind();
            }

    Regards.

    Batiste

    • Proposed as answer by Geetanjali Arora Friday, February 17, 2012 11:24 AM
    • Marked as answer by Qiao Wei Thursday, February 23, 2012 10:51 AM
    Friday, February 17, 2012 11:20 AM
  • The below function get the folder name and id of subfolder where items stored folder = item.folder.  Initially it will be null.

    static string GetParentFolder(SPListItem itemToFind, SPFolder folder) 

            {

                SPQuery query = new SPQuery();

                           query.Query = "<Where><Eq><FieldRef Name=\"ID\"/><Value Type=\"Integer\">"+ itemToFind.ID +"</Value></Eq></Where>";

                query.Folder = folder;

                query.ViewAttributes = "Scope=\"Recursive\"";

                SPListItemCollection items = itemToFind.ParentList.GetItems(query);

                int intpartentFolderID=0 ;

                if (items.Count > 0)

                {

                foreach (SPListItem item in items)

                {

                    SPFile f = item.Web.GetFile(item.Url);

                    string test11 = f.ParentFolder.Name;

                    intpartentFolderID = f.ParentFolder.Item.ID;

                   return (intpartentFolderID.ToString());

                           

                 }

                }

                return (intpartentFolderID.ToString());    

            } 
    Wednesday, January 7, 2015 2:31 PM