Search code examples
c#web-servicessharepointsharepoint-2010

Cannot get List's subfolders in Sharepoint


I need to get files from "Shared Folders" list. They are nested into several subfolders, it goes like this:

Shared Folders
 \- Incoming Documents
     \- Work Trips
         \- ####(code)
             \- the file I'm looking for

I used this question's answer's source code https://social.msdn.microsoft.com/Forums/office/en-US/16a2d993-2f5e-4242-8e5a-451a78c064a3/retrieving-folders-from-document-library?forum=sharepointdevelopmentlegacy and I'm not getting anything. I mean, when I'm passing the link - "http://SITE/DocLst/Входящие" from the "ows_EncodedAbsUrl" to the "QueryOptions - Folder" - I get just nothing in return when calling GetListItems. Zero rows.

What I can be doing wrong?

EDIT: Since people suspect microsoft's msdn is a "suspect link" here is my source code (slightly modified code from the link).

    static void FillNodes(string querytext, out XmlNode query, out XmlNode viewFields, out XmlNode queryOptions)
    {
        XmlDocument xmlDoc = new System.Xml.XmlDocument();
        xmlDoc.LoadXml(querytext);
        query = xmlDoc.SelectSingleNode("//Query");
        viewFields = xmlDoc.SelectSingleNode("//ViewFields");
        queryOptions = xmlDoc.SelectSingleNode("//QueryOptions");
    }

    static XmlNode GetFoldersNode(XmlNodeList oNodes)
    {
        // Find the node with root folders
        XmlNode folderNode = null;
        foreach (XmlNode node in oNodes)
        {
            if (node.ChildNodes.Count == 0)
                continue;
            folderNode = node;
            break;
        }
        if (folderNode == null)
            throw new Exception("Folders not found!");

        return folderNode;
    }
    static string FindFolder(XmlNode folderNode, string folderName)
    {
        string folderPath = null;
        foreach (XmlNode node in folderNode.ChildNodes)
        {
            if (node.Attributes == null || node.Attributes["ows_EncodedAbsUrl"] == null)
                continue;
            if (!node.Attributes["ows_EncodedAbsUrl"].Value.Contains(folderName))
                continue;
            folderPath = node.Attributes["ows_EncodedAbsUrl"].Value;
            break;
        }
        return folderPath;
    }

    static void Main(string[] args)
    {
        XmlDocument resdoc = new System.Xml.XmlDocument();
        XmlNode resnode = null;
        string strURL = "";
        string strFileName = "";

        sharepointsrv.Lists objLists = new sharepointsrv.Lists();
        objLists.Credentials = new System.Net.NetworkCredential("(login)", "(pw)", "(domain)");
        objLists.Url = "http://(site)/_vti_bin/lists.asmx";

        XmlNode ndQuery;
        XmlNode ndViewFields;
        XmlNode ndQueryOptions;
        FillNodes("<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>",
            out ndQuery, out ndViewFields, out ndQueryOptions);

        XmlNode ndListItems;
        XmlNodeList oNodes;
        XmlNode folderNode;
        string folderPath;
        // Get top level folders
        ndListItems = objLists.GetListItems("Общие документы", null, ndQuery, ndViewFields, null, ndQueryOptions, null); 
        oNodes = ndListItems.ChildNodes;

        // Find the node with root folders
        folderNode = GetFoldersNode(oNodes);

        // Find the "Входящие" folder
        folderPath = FindFolder(folderNode, "Входящие");

        FillNodes("<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>" + folderPath + "</Folder></QueryOptions></mylistitemrequest>",
            out ndQuery, out ndViewFields, out ndQueryOptions);

        // Get subfolder contents
        ndListItems = objLists.GetListItems("Общие документы", null, ndQuery, ndViewFields, null, ndQueryOptions, null);
        oNodes = ndListItems.ChildNodes;

        // At this point I am getting zero rows, even though I should be getting multiple folders
        // Following code digs deeper down the hierarchy of folders, but it's not working because nothing arrives at this point

        // Get node with folders
        folderNode = GetFoldersNode(oNodes);

        // Get "Командировки" folder
        folderPath = FindFolder(folderNode, "Командировки");

        // Get subfolder contents
        ndListItems = objLists.GetListItems("Общие документы", null, ndQuery, ndViewFields, null, ndQueryOptions, null);
        oNodes = ndListItems.ChildNodes;

        ...
    }

When I evaluate the oNodes after the "Get subfolder contents" code, I see zero rows, while there should be a list of subfolders of the "Входящие" folder.


Solution

  • Turns out, the example I was using from msdn was incorrect. Pasting the value of "ows_EncodedAbsUrl" into "Folder" queryOption will not work, because a relative path is required.

    "ows_EncodedAbsUrl" was returning "http://SITE/DocList/Folder" while "DocList/Folder" is required for the "Folder" option to work properly.