首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SPServices显示文档库中单个子文件夹的内容

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

Stack Overflow用户
提问于 2014-06-20 13:33:07
回答 3查看 8K关注 0票数 3

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

列表名称:共享文档

  • 文件夹#1
    • 子文件夹#4
      • 档案#6
      • 文件#7

代码语言:javascript
复制
- Subfolder #5 
    - File #8 

代码语言:javascript
复制
- File #9

  • 文件夹#2
  • 文件夹#3

用GetListItems列出顶级文件夹是很容易的,但是我想让用户点击其中一个文件夹和列表,只列出子文件夹的直接子文件夹。

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

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

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

有人能帮忙吗?

EN

回答 3

Stack Overflow用户

发布于 2014-06-21 09:49:29

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

代码语言:javascript
复制
<QueryOptions>
     <Folder>/SiteName/Lists/Links/folder/subFolder</Folder>
</QueryOptions>

示例

代码语言:javascript
复制
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);
      });
    }
  });

请跟随这条线了解更多细节。

票数 3
EN

Stack Overflow用户

发布于 2014-06-20 18:21:42

您可以使用名为SPServices的JavaScript API,而不是SharepointPlus (如果希望保留SPServices,请参见末尾)。做你想做的事有两种方法:

  1. 把你图书馆的所有结构都整理好,
  2. 或者只获取用户单击的文件夹。

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

代码语言:javascript
复制
$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":

代码语言:javascript
复制
$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"))
  }
});

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

代码语言:javascript
复制
<QueryOptions>
  <ViewAttributes Scope="RecursiveAll"></ViewAttributes>
  <Folder>http://your.siteweb.com/path/to/site/collection/path/to/Folder #1</Folder>
</QueryOptions>

要知道要使用哪个ViewAttributes范围使用你可以检查我的代码,请执行以下操作。

票数 1
EN

Stack Overflow用户

发布于 2014-09-05 19:38:50

若要排列所有文件夹、子文件夹和文件的顺序,请尝试使用以下函数:

代码语言:javascript
复制
<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>

如果您想要手风琴功能,请尝试向代码中添加这种引导行为

诚挚的问候。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24328532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档