locked
Get all Folder names using GetListItems()

    Question

  • Hi All,

    I want to get all the Folder names in the Document Library.

    List Name
     Folder1    Folder2    Folder3
       Folder1.1                 Folder3.1   Folder3.2
          Folder1.1.1              Folder3.1.1
                                           Folder3.1.1.1


    I have list in this fashion.

    I have used QueryOptions as "Recursive"\ but it gets all the items (or) documents inside the Sub Folders. But i need all the Folder names including Sub Folders. 

    Can anyone pls help me with this issue.....?

            
    Friday, July 10, 2009 2:35 PM

Answers

  • Definitely, that <ViewAttributes Scope='RecursiveAll'/> is work with GetListItems of Lists webservice when we used with queryoptions.

    Try the following to retrieve all the folders within list or Library using Lists webservice
     

    XmlDocument xmlDoc = new XmlDocument();

    XmlNode query = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
    XmlNode viewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
    XmlNode queryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");

    queryOptions.InnerXml = @"<IncludeMandatoryColumns>TRUE</IncludeMandatoryColumns>
                              <ViewAttributes Scope='RecursiveAll'/>
                              <DateInUtc>TRUE</DateInUtc>";            
    viewFields.InnerXml = "";
    query.InnerXml = @"<Where>
                       <Eq>
                       <FieldRef Name='FSObjType' />
                       <Value Type='Lookup'>1</Value>
                       </Eq>
                       </Where>";            

    XmlNode ndListItems = ListService.GetListItems("Shared Documents", null,query ,viewFields , null,queryOptions , null);
                
    xmlDoc.LoadXml(ndListItems.OuterXml);
    XmlNodeList folders = xmlDoc.GetElementsByTagName("z:row");
    string folderDetails = "";

    foreach (XmlNode folder in folders)
    {
       folderDetails += "Title: "+folder.Attributes["ows_Title"].Value +", URL: "+folder.Attributes["ows_EncodedAbsUrl"].Value+ "\n";
    }
    MessageBox.Show(folderDetails);


    Shantha Kumar T - MCTS
    Saturday, July 11, 2009 2:14 PM

All replies

  • Using the Lists.asmx web service you will have to call GetListItems multiple times to get a folder's subfolder names. The following post contains code to do this:

    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/16a2d993-2f5e-4242-8e5a-451a78c064a3


    certdev.com
    Friday, July 10, 2009 4:37 PM
  • Hi hema123456,

    Check the follwoing url, this will help you in getting all the folders from Document Library.

    If you want to retrive all the items with folders, you have to use Scope value as "RecursiveAll".



    Shantha Kumar - MCTS 
    Friday, July 10, 2009 6:34 PM
  • The RecursiveAll scope will not return subfolders.
    certdev.com
    Friday, July 10, 2009 9:16 PM
  • I tried that, RecursiveAll property is working and it returns all the items and subfolders under the list.

    query.ViewAttributes = "Scope='RecursiveAll'";


    Shantha Kumar - MCTS 
    Saturday, July 11, 2009 5:50 AM
  • It will not work with the Lists web service and the QueryOptions when calling GetListItems.
    certdev.com
    Saturday, July 11, 2009 1:30 PM
  • Definitely, that <ViewAttributes Scope='RecursiveAll'/> is work with GetListItems of Lists webservice when we used with queryoptions.

    Try the following to retrieve all the folders within list or Library using Lists webservice
     

    XmlDocument xmlDoc = new XmlDocument();

    XmlNode query = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
    XmlNode viewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
    XmlNode queryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");

    queryOptions.InnerXml = @"<IncludeMandatoryColumns>TRUE</IncludeMandatoryColumns>
                              <ViewAttributes Scope='RecursiveAll'/>
                              <DateInUtc>TRUE</DateInUtc>";            
    viewFields.InnerXml = "";
    query.InnerXml = @"<Where>
                       <Eq>
                       <FieldRef Name='FSObjType' />
                       <Value Type='Lookup'>1</Value>
                       </Eq>
                       </Where>";            

    XmlNode ndListItems = ListService.GetListItems("Shared Documents", null,query ,viewFields , null,queryOptions , null);
                
    xmlDoc.LoadXml(ndListItems.OuterXml);
    XmlNodeList folders = xmlDoc.GetElementsByTagName("z:row");
    string folderDetails = "";

    foreach (XmlNode folder in folders)
    {
       folderDetails += "Title: "+folder.Attributes["ows_Title"].Value +", URL: "+folder.Attributes["ows_EncodedAbsUrl"].Value+ "\n";
    }
    MessageBox.Show(folderDetails);


    Shantha Kumar T - MCTS
    Saturday, July 11, 2009 2:14 PM
  • Thanks Shantha. I stand corrected. We all learn something new every day.
    certdev.com
    Saturday, July 11, 2009 5:13 PM
  • Thank You Shanta Kumar.
    Monday, July 13, 2009 5:46 AM