使用MS Graph Api在一次调用中创建整个文件夹结构



我遇到一种情况,需要以编程方式将一个文件夹及其所有子文件夹(包括其子文件夹(创建到sharepoint文档库中。一个电话就能做到吗?

现在我一个文件夹接一个文件夹地做这件事,因为有很多子文件夹,所以确实需要很长时间。以下是我的操作方法:

//newFolder - The folder that i want to create, contains subfolders
//destinationFolder - The destination folder where i want to create newFolder
public void createFolder(ExternalDocumentFolder newFolder, ExternalDocumentFolder destinationFolder) {
GraphServiceClient<Request> graphClient = graphServiceClientBuilder.buildForNoUser();
String driveID = getDriveID(graphClient);
//All subfolders are flattened into a single list for easy of saving
List<ExternalDocumentFolder> externalDocumentFolders = flattenFolder(newFolder);
for (ExternalDocumentFolder folder : externalDocumentFolders) {
DriveItem newDriveItem = mapToDriveItem(folder);
String destinationPath = destinationFolder.getPath();
if(folder.getParent() != null){
destinationPath = destinationPath + "/" + folder.getParent().getPath();
}
DriveItem returnedDriveItem = graphClient.drives(driveID).items("root:/" + destinationPath + ":").children().buildRequest().post(newDriveItem);
}
}

您可以使用批处理请求将所有请求合并为一个调用。请检查这份文件。

您可以复制文件夹及其结构,我们已经做了几个月了,没有出现任何问题。希望这能帮助到别人。

$url = 'drives/DRIVE_ID/items/FOLDER_COPY_ID/copy';
$guzzle = new GuzzleHttpClient();
$create = $guzzle->request('POST', 'https://graph.microsoft.com/v1.0/' . $url, [
"headers" => [
"Content-Type" => "application/json",
"Accept" => "application/json",
"Authorization" => "Bearer ACCESS_TOKEN
],
"json" => [
"name" => DESTINATION_FOLDER_NAME,
"folder" => new stdClass(),
"@microsoft.graph.conflictBehavior" => "fail",
"parentReference" => [
"driveId" => DRIVE_ID,
"id" => DESTINATION_FOLDER_ID
]
]
]);
$response = $create->getStatusCode();
// handle response

参考编号:https://learn.microsoft.com/en-us/graph/api/driveitem-copy?view=graph-rest-1.0&tabs=http

现在可以通过一个批处理调用创建文件夹结构。我不确定这是否是最近添加的,但可以这样做:

文件夹1/文件夹2/文件夹3/文件夹4

$folders = [
"id" => 1,
"method" => "POST",
"url" => "/drives/DRIVE_ID/root:/Folder1/Folder2/Folder3:/children",
"body" => [
"name" => 'Folder4',
"folder" => new stdClass(),
"@microsoft.graph.conflictBehavior" => "fail"
],
"headers" => [
"Content-Type" => "application/json"
]
];
$guzzle = new GuzzleHttpClient();
$create = $guzzle->request("POST", "https://graph.microsoft.com/v1.0/$batch", [
"headers" => [
"Content-Type" => "application/json",
"Accept" => "application/json",
"Authorization" => "Bearer ACCESS_TOKEN"
],
"json" => [
"requests" => [$folders]
]
]);
$response = $create->getStatusCode();

希望这能帮助到别人。确保";name";param包含最后一个文件夹。看起来效果不错。

相关内容

  • 没有找到相关文章

最新更新