Home > Back-end >  Creating a entire folder structure in one call using MS Graph-Api
Creating a entire folder structure in one call using MS Graph-Api

Time:03-19

I have a situation where i need to programmatically create a folder and all its subfolders including their subfolders into a sharepoint documment library. Is it possibible to do that in 1 call?

Right now i do it folder by folder which does take a noticeable amount of time since there are many subfolders. Here is how i do it:

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

CodePudding user response:

You can use a batch request to combine all the requests into a single call. Please check this document.

  • Related