除了 SPServices,您可以使用我的称为SharepointPlus的 JavaScript API (如果您想保留 SPServices,请参阅最后)。两种方法可以做你想做的事:
- 获取您图书馆的所有结构,
- 或仅获取用户单击的文件夹
使用第一个选项,您只能调用以下代码一次,然后您必须将结构存储在某处:
$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,您可以查看我的代码。