【问题标题】:Creating a entire folder structure in one call using MS Graph-Api使用 MS Graph-Api 在一次调用中创建整个文件夹结构
【发布时间】:2022-07-29 16:24:58
【问题描述】:

我有一种情况,我需要以编程方式将文件夹及其所有子文件夹(包括它们的子文件夹)创建到共享点文档库中。 是否有可能在 1 次通话中做到这一点?

现在我一个文件夹一个文件夹地做,因为有很多子文件夹,这确实需要相当长的时间。这是我的做法:

//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);
    }
}

【问题讨论】:

  • 您可以使用批处理请求将所有请求合并到一个调用中。请检查此document
  • @ShivaKeshavVarma 它奏效了。如果你愿意,请回答我可以接受它
  • 很高兴听到它对您有用。 :)

标签: java spring sharepoint microsoft-graph-api directory-structure


【解决方案1】:

您可以使用批处理请求将所有请求合并到一个调用中。请检查此document

【讨论】:

  • 嗯,实际上,虽然它确实有效,并且将所需时间减少了一半,但仍然不够好。微软真的不提供创建整个文件夹结构的选项吗?
  • AFAIK,他们没有那个选项,但你可以在 Ideas site 请求这种功能,以便将来可以使用
【解决方案2】:

您可以复制文件夹及其结构,我们已经这样做了几个月,没有任何问题。希望这对某人有所帮助。

$url = 'drives/DRIVE_ID/items/FOLDER_COPY_ID/copy';
$guzzle = new \GuzzleHttp\Client();
$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    相关资源
    最近更新 更多