3

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

列表名称:共享文档

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

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

因此,如果有人单击“文件夹 #1”,那么我只想向他们显示以下内容:

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

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

任何人都可以帮忙吗?

4

3 回答 3

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);
      });
    }
  });

请关注此线程以获取更多详细信息。

于 2014-06-21T09:49:29.097 回答
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>

如果您想要手风琴功能,请尝试将此引导行为添加到代码中。

此致。

于 2014-09-05T19:38:50.527 回答
1

除了 SPServices,您可以使用我的称为SharepointPlus的 JavaScript API (如果您想保留 SPServices,请参阅最后)。两种方法可以做你想做的事:

  1. 获取您图书馆的所有结构,
  2. 或仅获取用户单击的文件夹

使用第一个选项,您只能调用以下代码一次,然后您必须将结构存储在某处:

$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 Scope,您可以查看我的代码

于 2014-06-20T18:21:42.577 回答