使用SPServices从文档库显示单个子文件夹的内容

时间:2014-06-20 13:33:07

标签: sharepoint spservices

在SharePoint和使用SPServices中,我试图查看文档库的特定子文件夹的文件夹内容(而不是库中的每个文件和文件夹)。库结构如下所示:

列表名称:共享文档

  • 文件夹#1
    • 子文件夹#4
      • 档案#6
      • 文件#7
    • 子文件夹#5
      • 文件#8
    • 文件#9
  • 文件夹#2
  • 文件夹#3

使用GetListItems列出顶级文件夹很容易,但我想让用户点击其中一个文件夹并列出并仅列出子文件夹的直接子项。

所以,如果有人点击"文件夹#1"那么我只想向他们展示以下内容:

  • 文件夹#4
  • 文件夹#5
  • 文件#9

我完全不知道如何列出特定子文件夹的直接子项。

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:3)

为了限制对特定文件夹的查询,可以指定CAMLQueryOptions参数Folder

<QueryOptions>
     <Folder>/SiteName/Lists/Links/folder/subFolder</Folder>
</QueryOptions>

实施例

var listName = "Shared Documents";
var folderName = "/Shared Documents/Folder #1";

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: listName,
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    CAMLQueryOptions: "<QueryOptions><Folder>" + folderName + "</Folder></QueryOptions>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var title = $(this).attr("ows_Title");
        console.log(title);
      });
    }
  });

请按this thread了解详情。

答案 1 :(得分:1)

您可以使用名为SharepointPlus的JavaScript API代替SPServices(如果您想保留SPServices,请参阅最后的内容)。 两种方法可以做你想做的事:

  1. 获取图书馆的所有结构,
  2. 或只获取用户点击的文件夹
  3. 使用第一个选项,您只能调用以下代码一次,然后您必须将结构存储在某处:

    $SP().list("Shared Documents Library").get({
      fields:"BaseName,FileRef,FSObjType", // "BaseName" is the name of the file/folder; "FileRef" is the full path of the file/folder; "FSObjType" is 0 for a file and 1 for a folder (you need to apply $SP().cleanResult())
      folderOptions:{
        show:"FilesAndFolders_Recursive"
      }
    }, function(files) {
      // in "files" you have ALL the files and folders in your library
      for (var i=0; i<files.length; i++) {
        console.log(files[i].getAttribute("FileRef"))
      }
    });
    

    对于第二个选项,每次用户点击文件夹时,您都必须调用以下代码。例如,如果他点击“文件夹#1”:

    $SP().list("Shared Documents Library").get({
      fields:"BaseName,FileRef,FSObjType", // "BaseName" is the name of the file/folder; "FileRef" is the full path of the file/folder; "FSObjType" is 0 for a file and 1 for a folder (you need to apply $SP().cleanResult())
      folderOptions:{
        show:"FilesAndFolders_InFolder", /* this option changed from 1) */
        path:"Folder #1" /* here is the name of the folder */
      }
    }, function(files) {
      // it will get the files and folders into the "Folder #3"
      for (var i=0; i<files.length; i++) {
        console.log(files[i].getAttribute("FileRef"))
      }
    });
    

    如果您不想使用带有SPServices的SharepointPlus,则必须为查询定义queryOptions。如果您使用选项1(上方),则 queryOptions 将如下所示:

    <QueryOptions>
      <ViewAttributes Scope="RecursiveAll"></ViewAttributes>
      <Folder>http://your.siteweb.com/path/to/site/collection/path/to/Folder #1</Folder>
    </QueryOptions>
    

    要知道要使用哪个ViewAttributes范围you can check my code

答案 2 :(得分:1)

要输入所有文件夹,子文件夹和文件的顺序,请尝试使用此功能:

<script>
$(document).ready(function(){
    var list = "Shared Documents";
    var url = "Shared Documents";    
    createTree(url, 0, list);

 });



function createTree(url, ID, list){
    //get the url to define the level of tree,
    $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: list,
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='ID' /></ViewFields>",
    CAMLQueryOptions: "<QueryOptions><Folder>" + url + "</Folder></QueryOptions>",
    completefunc: function (xData, Status) {
    $("#"+ID+"").append("<ul id='prime"+ID+"'>");
    $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var id = $(this).attr("ows_ID");
        var title = $(this).attr("ows_Title");      
        var folder =   $(this).attr("ows_FileLeafRef").split(";#")[1]; 
       //var ref =   $(this).attr("ows_FileRef").split(";#")[1]; //this field gets the folder or file url

        $("#prime"+ID+"").append("<li id='"+id+"'>" +
                                    folder +
                                    //', Link: '+ ref +
                                    "  [" +
                                    $(this).attr("ows_FileLeafRef") +
                                    " :: " +
                                    $(this).attr("ows_FSObjType") +
                                    "]" +
                                "</li>");                               

     var thisFSObjType = $(this).attr("ows_FSObjType").split(";#")[1];
     if(thisFSObjType == 1) {
        //auto call the function createTree again, to get subfolders and files, assigning a new url/subfolder and id
        createTree(url+'/'+folder, id, list);         
      }
      });
      $("#"+ID+"").append("</ul>");
    }
 }); 
}

</script>
<body>
  <div><h1>My Tree</h1></div>
  <div id="0">
  </div>
</body>

如果您想要手风琴功能,请尝试将this bootstrap behavior添加到代码中。

最诚挚的问候。

相关问题