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

Creating a entire folder structure in one call using MS Graph-Api

我遇到一种情况,我需要以编程方式将文件夹及其所有子文件夹(包括它们的子文件夹)创建到 Sharepoint 文档库中。 是否可以在 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.