无法在Sharepoint中获取List的子文件夹

时间:2015-07-31 15:10:33

标签: c# web-services sharepoint sharepoint-2010

我需要从“共享文件夹”列表中获取文件。它们嵌套在几个子文件夹中,它是这样的:

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

我使用了这个问题的答案的源代码https://social.msdn.microsoft.com/Forums/office/en-US/16a2d993-2f5e-4242-8e5a-451a78c064a3/retrieving-folders-from-document-library?forum=sharepointdevelopmentlegacy而我没有得到任何东西。我的意思是,当我将链接 - “http://SITE/DocLst/Входящие”从“ows_EncodedAbsUrl”传递给“QueryOptions - Folder”时 - 在调用GetListItems时,我得不到任何回报。零行。

我可能做错了什么?

编辑:由于人们怀疑微软的msdn是一个“可疑链接”,这里是我的源代码(链接略有修改的代码)。

    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;

        ...
    }

当我在“获取子文件夹内容”代码后评估oNodes时,我看到零行,而应该有一个“Входящие”文件夹的子文件夹列表。

1 个答案:

答案 0 :(得分:0)

事实证明,我在msdn中使用的示例不正确。粘贴&#34; ows_EncodedAbsUrl&#34;的价值进入&#34;文件夹&#34; queryOption不起作用,因为需要相对路径。

&#34; ows_EncodedAbsUrl&#34;正在回归&#34; http://SITE/DocList/Folder&#34;而#34; DocList / Folder&#34; &#34;文件夹&#34;是必需的。选项工作正常。