locked
Retrieving folders from document library

    Question

  • Hii all,

    i want to retieve all the folders from the sharepoint document library using sharepoint web serives. Code will get executed from other than sharepoint machine.

    Saturday, May 09, 2009 12:18 PM

Answers

  • In order to retrieve all folders in a sharepoint document library you will need to make a recursive call to get folders within folders. You want to make sure not to make unecessary web service calls. The best way is to make an initial query to get all top level folders in the document library by including in your where clause the test for FSObjType = 1. This will return folders only. Next you can iterate through this returned Datatable  and make a recursive call to get the sub folders. This will merge the results into to the Datatable sent in as an argument.  The following code has two methods one for the initial query and one for the sub folders.

    public static DataTable GetListItemJustFolders()
            {
                string query = "<mylistitemrequest><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Lookup\">1</Value></Eq></Where></Query><ViewFields><FieldRef Name=\"EncodedAbsUrl\"/><FieldRef Name=\"ID\" /><FieldRef Name=\"FileRef\" /><FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" /></ViewFields><QueryOptions></QueryOptions></mylistitemrequest>";
                DataTable dt = null;
    
                using(listservice.Lists listProxy = new listservice.Lists())
                {
    
                    listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
                    listProxy.UseDefaultCredentials = true;
    
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(query);
    
                    XmlNode queryNode = doc.SelectSingleNode("//Query");
                    XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
                    XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");
    
                    XmlNode retNode = listProxy.GetListItems("Shared Documents", string.Empty, queryNode, viewNode, string.Empty, optionNode, "e5bc34ff-6cde-4fee-aa4c-356baa57b37b");
    
                    DataSet ds = new DataSet();
                    using (StringReader sr = new StringReader(retNode.OuterXml))
                        ds.ReadXml(sr);
    
                    if (ds.Tables["Row"] != null && ds.Tables["Row"].Rows.Count > 0)
                    {
                        var folderUrls = from f in ds.Tables["Row"].AsEnumerable() select f["ows_EncodedAbsUrl"];
                        dt = ds.Tables["Row"].Copy();
    
                        foreach (string folderUrl in folderUrls)
                        {
                            GetListItemSubFolders(folderUrl, dt);
                        }
    
                    }
    
                }
    
                return dt;
    
            }
    
            public static void GetListItemSubFolders(string parentFolder, DataTable retTable)
            {
    
                string query = "<mylistitemrequest><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Lookup\">1</Value></Eq></Where></Query><ViewFields><FieldRef Name=\"EncodedAbsUrl\"/><FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" /></ViewFields><QueryOptions><Folder>" + parentFolder + "</Folder></QueryOptions></mylistitemrequest>";
                DataTable dt = null;
    
                using (listservice.Lists listProxy = new listservice.Lists())
                {
                    listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
                    listProxy.UseDefaultCredentials = true;
    
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(query);
    
                    XmlNode queryNode = doc.SelectSingleNode("//Query");
                    XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
                    XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");
    
                    XmlNode retNode = listProxy.GetListItems("Shared Documents", string.Empty, queryNode, viewNode, string.Empty, optionNode, "e5bc34ff-6cde-4fee-aa4c-356baa57b37b");
    
                    DataSet ds = new DataSet();
                    using(StringReader sr = new StringReader(retNode.OuterXml))
                        ds.ReadXml(sr);
    
    
                    if (ds.Tables["Row"] != null && ds.Tables["Row"].Rows.Count > 0)
                    {
                        var folderUrls = from f in ds.Tables["Row"].AsEnumerable() select f["ows_EncodedAbsUrl"];
                        dt = ds.Tables["Row"].Copy();
    
                        foreach (string folderUrl in folderUrls)
                        {
                            GetListItemSubFolders(folderUrl, dt);
                        }
    
                        retTable.Merge(dt);
    
                    }
                
                }
    
             
            }

    http://www.certdev.com
    Saturday, May 09, 2009 10:18 PM

All replies

  • Lists objLists = new Lists(); 
                                        objLists.Credentials = System.Net.CredentialCache.DefaultCredentials;
                                        objLists.Url = urD+"/_vti_bin/lists.asmx"; // change the URL to your sharepoint site
                                        XmlDocument xmlDoc = new System.Xml.XmlDocument();
                                        XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
                                        XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields","");
                                        XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element,"QueryOptions", "");
                                        ndQueryOptions.InnerXml ="<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls>";
                                        ndViewFields.InnerXml = "";
                                        ndQuery.InnerXml = "";
                                        try
                                        {

                                            XmlNode ndListItems = objLists.GetListItems(param.Split(new char[] { '#' })[1], null, ndQuery, ndViewFields, null, ndQueryOptions, null); // you can change the document library name to your custom document library name 

                                            XmlNodeList oNodes = ndListItems.ChildNodes;
                                            foreach (XmlNode node in oNodes)
                                            {
                                                XmlNodeReader objReader = new XmlNodeReader(node);
                                                while (objReader.Read())
                                                {
                                                    if (objReader["ows_EncodedAbsUrl"] != null && objReader["ows_LinkFilename"] != null)
                                                    {
                                                        strURL = objReader["ows_EncodedAbsUrl"].ToString();
                                                        strFileName = objReader["ows_LinkFilename"].ToString();
                                                       object o =web.GetFileOrFolderObject(strURL);
                                      if(o is SPFolder)
                                     {
                                         //Have Folder name
                                     }
                                                    }
                                                }
                                            }

    Hope this help

    André Lage Microsoft SharePoint and CRM Consultant http://aaclage.blogspot.com
    Saturday, May 09, 2009 1:25 PM
  • In order to retrieve all folders in a sharepoint document library you will need to make a recursive call to get folders within folders. You want to make sure not to make unecessary web service calls. The best way is to make an initial query to get all top level folders in the document library by including in your where clause the test for FSObjType = 1. This will return folders only. Next you can iterate through this returned Datatable  and make a recursive call to get the sub folders. This will merge the results into to the Datatable sent in as an argument.  The following code has two methods one for the initial query and one for the sub folders.

    public static DataTable GetListItemJustFolders()
            {
                string query = "<mylistitemrequest><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Lookup\">1</Value></Eq></Where></Query><ViewFields><FieldRef Name=\"EncodedAbsUrl\"/><FieldRef Name=\"ID\" /><FieldRef Name=\"FileRef\" /><FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" /></ViewFields><QueryOptions></QueryOptions></mylistitemrequest>";
                DataTable dt = null;
    
                using(listservice.Lists listProxy = new listservice.Lists())
                {
    
                    listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
                    listProxy.UseDefaultCredentials = true;
    
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(query);
    
                    XmlNode queryNode = doc.SelectSingleNode("//Query");
                    XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
                    XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");
    
                    XmlNode retNode = listProxy.GetListItems("Shared Documents", string.Empty, queryNode, viewNode, string.Empty, optionNode, "e5bc34ff-6cde-4fee-aa4c-356baa57b37b");
    
                    DataSet ds = new DataSet();
                    using (StringReader sr = new StringReader(retNode.OuterXml))
                        ds.ReadXml(sr);
    
                    if (ds.Tables["Row"] != null && ds.Tables["Row"].Rows.Count > 0)
                    {
                        var folderUrls = from f in ds.Tables["Row"].AsEnumerable() select f["ows_EncodedAbsUrl"];
                        dt = ds.Tables["Row"].Copy();
    
                        foreach (string folderUrl in folderUrls)
                        {
                            GetListItemSubFolders(folderUrl, dt);
                        }
    
                    }
    
                }
    
                return dt;
    
            }
    
            public static void GetListItemSubFolders(string parentFolder, DataTable retTable)
            {
    
                string query = "<mylistitemrequest><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Lookup\">1</Value></Eq></Where></Query><ViewFields><FieldRef Name=\"EncodedAbsUrl\"/><FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" /></ViewFields><QueryOptions><Folder>" + parentFolder + "</Folder></QueryOptions></mylistitemrequest>";
                DataTable dt = null;
    
                using (listservice.Lists listProxy = new listservice.Lists())
                {
                    listProxy.Url = "http://basesmcdev2/sites/tester1/_vti_bin/lists.asmx";
                    listProxy.UseDefaultCredentials = true;
    
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(query);
    
                    XmlNode queryNode = doc.SelectSingleNode("//Query");
                    XmlNode viewNode = doc.SelectSingleNode("//ViewFields");
                    XmlNode optionNode = doc.SelectSingleNode("//QueryOptions");
    
                    XmlNode retNode = listProxy.GetListItems("Shared Documents", string.Empty, queryNode, viewNode, string.Empty, optionNode, "e5bc34ff-6cde-4fee-aa4c-356baa57b37b");
    
                    DataSet ds = new DataSet();
                    using(StringReader sr = new StringReader(retNode.OuterXml))
                        ds.ReadXml(sr);
    
    
                    if (ds.Tables["Row"] != null && ds.Tables["Row"].Rows.Count > 0)
                    {
                        var folderUrls = from f in ds.Tables["Row"].AsEnumerable() select f["ows_EncodedAbsUrl"];
                        dt = ds.Tables["Row"].Copy();
    
                        foreach (string folderUrl in folderUrls)
                        {
                            GetListItemSubFolders(folderUrl, dt);
                        }
    
                        retTable.Merge(dt);
    
                    }
                
                }
    
             
            }

    http://www.certdev.com
    Saturday, May 09, 2009 10:18 PM
  • thanks steve and André,

    that solved my problem.

    Nitin Khubani
    Sunday, May 10, 2009 12:06 PM