1

As part of a user action, we are using the MS Graph Java SDK to first list all permissions of a file, then iterating over the list of permissions to delete each one individually. This seems to have some performance issues. We were wondering if there is any way to batch the calls using the IGraphServiceClient.

Relevant APIs used:

1 Answers1

0

You can make batch requests.

1. Create MSBatch Request Steps (examples below)

Request requestGetMe = new Request.Builder().url("https://graph.microsoft.com/v1.0/me/").build();
List<String> arrayOfDependsOnIdsGetMe = null;
MSBatchRequestStep stepGetMe = new MSBatchRequestStep("1", requestGetMe, arrayOfDependsOnIdsGetMe);
Request requestGetMePlannerTasks = new Request.Builder().url("https://graph.microsoft.com/v1.0/me/planner/tasks").build();
List<String> arrayOfDependsOnIdsGetMePlannerTasks = Arrays.asList("1");
MSBatchRequestStep stepMePlannerTasks = new MSBatchRequestStep("2", requestGetMePlannerTasks, arrayOfDependsOnIdsGetMePlannerTasks);
String body = "{" + 
        "\"displayName\": \"My Notebook\"" + 
        "}";
RequestBody postBody = RequestBody.create(MediaType.parse("application/json"), body);
Request requestCreateNotebook = new Request
    .Builder()
        .addHeader("Content-Type", "application/json")
    .url("https://graph.microsoft.com/v1.0/me/onenote/notebooks")
    .post(postBody)
    .build();
MSBatchRequestStep stepCreateNotebook = new MSBatchRequestStep("3", requestCreateNotebook, Arrays.asList("2"));

2. Create MSBatch Request Content and get content

List<MSBatchRequestStep> steps = Arrays.asList(stepGetMe, stepMePlannerTasks, stepCreateNotebook);
MSBatchRequestContent requestContent = new MSBatchRequestContent(steps);
String content = requestContent.getBatchRequestContent();

3. Make call to $batch endpoint

OkHttpClient client = HttpClients.createDefault(auth);
Request batchRequest = new Request
    .Builder()
    .url("https://graph.microsoft.com/v1.0/$batch")
    .post(RequestBody.create(MediaType.parse("application/json"), content))
    .build();
Response batchResponse = client.newCall(batchRequest).execute();

4. Create MSBatch Response Content

MSBatchResponseContent responseContent = new MSBatchResponseContent(batchResponse);
Response responseGetMe = responseContent.getResponseById("1");
// Use the response of each request
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • Is there a way to make these batch requests using the IGraphServiceClient rather than the OkHttpClient though? – Bill Collab Jan 26 '20 at 23:30
  • @BillCollab There should be. We added support for that in the .NET library. I'll need to verify if we did it with Java one also. Having said that the OkHttpClient will still have the Graph middleware to handle throttling and auth. – Darrel Miller Jan 29 '20 at 02:09