我正在尝试使用 REST api 在某个共享点站点下查找文件夹的结构或所有文件和文件夹的列表。有没有办法使用api获取“导出到excel”表中的信息?
问问题
1049 次
1 回答
0
您可以通过列出文件夹并递归检查更多文件夹来完成
const getFolderHierarchy = async (siteUrl, subsite, folder, authorization) => {
logger.info('Fetching Hierarchy'.padEnd(padGap), folder.cyan);
const folderPath = `('${makePath(null, subsite)}/${folder}')?$expand=Folders,Files`;
let response = null;
try {
response = await fetch(`${makePath(siteUrl, subsite)}/_api/Web/GetFolderByServerRelativeUrl${folderPath}`, {
method: 'GET',
body: null,
headers: getHeaders(authorization),
}).catch(throwError);
let folders = [folder];
response = await response.json().catch(throwError);
if (response.d.Folders.results.length) {
for (const subFolder of response.d.Folders.results) {
if (subFolder.Name !== 'Forms') { // Ignore forms
folders.push(`${folder}/${subFolder.Name}`);
folders = folders.concat(
await getFolderHierarchy(siteUrl, subsite, `${folder}/${subFolder.Name}`, authorization)
.catch(throwError),
);
}
}
}
response = [...new Set(folders)];
} catch (e) {
throwError(e);
}
return response;
};
您将需要此方法列出特定文件夹中的所有文件
const getFiles = async (siteUrl, subsite, folder, authorization) => {
logger.info('Listing Folder'.padEnd(padGap), folder.cyan);
let response = null;
try {
response = await fetch(`${makePath(siteUrl, subsite)}/_api/web/getFolderByServerRelativeUrl('${folder}')/files`, {
method: 'GET',
body: null,
headers: getHeaders(authorization),
}).catch(throwError);
response = await response.json().catch(throwError);
response = response.d.results.map((file) => file.ServerRelativeUrl);
} catch (e) {
throwError(e);
}
return response;
};
然后,当您有一个列表时,您可以迭代并列出所有文件
const folders = await getFolderHierarchy(siteUrl, subsite, remoteFolder, authorization).catch(throwError);
for (const folder of folders) {
const files = await getFiles(siteUrl, subsite, folder, authorization).catch(throwError);
for (const file of files) {
//do something with the file
}
}
你可以在这里找到完整的源代码
于 2019-08-24T05:43:19.227 回答