前言
近期有一个获取文档库中所有文件的需求,且文件层级较多,文件数量较大,经过多次尝试,最后用了SharePoint Plus js来实现
代码
<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("DocName").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" />
说明:folderOptions.show有四个可选值,如下
1、 "FilesOnly_Recursive" that lists all the files recursively from the provided path (and its children); 获取所有文件包含子文件夹中的文件
2、"FilesAndFolders_Recursive" that lists all the files and folders recursively from the provided path (and its children);获取所有文件夹和文件包含子文件夹中的文件夹和文件
3、"FilesOnly_InFolder" that lists all the files from the provided path;获取指定文件夹中的文件
4、"FilesAndFolders_InFolder" that lists all the files and folders from the provided path获取指定文件夹中的文件夹和文件
分页查询
需要注意的是,如果文件或数据item大于5000条,则需要启用分页查询,因为该接口一次性最多可以返回5000条数据,代码如下
$SP().list('list name').get({
fields: "Id,Title",
rowlimit: 4900,
paging: true,
progress: function progress(nbItemsLoaded) {
// for each new page this function will be called
}
}).then(function (data) {
console.log(data)
});