【问题标题】:How to get folder names of list in Sharepoint using javascript?如何使用 javascript 在 Sharepoint 中获取列表的文件夹名称?
【发布时间】:2020-05-05 10:44:15
【问题描述】:
对于共享点请求,我正在使用共享点库 https://aymkdn.github.io/SharepointPlus/。我正在努力获取列表的文件夹名称。以及如何在列表中的子文件夹中获取文件夹名称?
如果我尝试在列表上提出请求,我只会获得有关文件夹内文件的数据。但是,如果我有没有文件的文件夹怎么办,我无法获得该文件夹的名称。
$SP().list("TestList").get().then(function(data) {console.log(data) });
【问题讨论】:
标签:
javascript
sharepoint
【解决方案1】:
示例演示脚本(如果要区分文件夹/文件,请检查 FSObjType):
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharepointplus/browser/sharepointplus.js"></script>
<script type="text/javascript">
function getFolderAndFilesInFolder() {
// if you want to list only the files visible into a folder for a Document Library
$SP().list("MyDoc2").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()), "File_x0020_Size" the filesize in bytes
folderOptions: {
path: "folder",
show: "FilesAndFolders_InFolder"
}
}).then(function (items) {
for (var i = 0; i < items.length; i++) {
console.log(items[i].getAttribute("FileRef"));
console.log(items[i].getAttribute("FileLeafRef"));
//console.log(items[i].getAttribute("FSObjType"));
}
})
}
</script>
<input id="Button1" type="button" onclick="getFolderAndFilesInFolder()" value="button" />
[setup.folderOptions.show="FilesAndFolders_InFolder"] 四个值: "FilesOnly_Recursive" 从提供的路径(及其子路径)递归列出所有文件; “FilesAndFolders_Recursive”,从提供的路径(及其子路径)递归列出所有文件和文件夹; “FilesOnly_InFolder”列出了提供路径中的所有文件; “FilesAndFolders_InFolder”列出了提供路径中的所有文件和文件夹