Implement JSON Batching in Microsoft Graph Requests

Introduction

Microsoft Graph is the gateway to data and intelligence in Microsoft 365. It provides a unified programmability model that you can use to access the tremendous amount of data in Office 365, Windows 10, and Enterprise Mobility + Security.It offers a single endpoint, https://graph.microsoft.com, to provide access to rich, people-centric data and insights exposed as resources of Microsoft 365 services.

While making the requests to resources using Graph, it’s quite essential to ensure that the calls are optimized and performance is taken care of while using it in an enterprise application. When we have to deal with multiple graph calls in the application, JSON batching allows you to optimize your application by combining multiple requests (up to 20) into a single JSON object. This way the number of round trips for multiple calls can be reduced by batching it into a single call.

Scenario

Let’s say that we are trying to achieve 4 operations and we have been making individual calls for all of them

  • Create a SharePoint List in the Site
  • Intimate the user about the list creation
  • Rename a file in the document library
  • Remove specific permission from the document library

In this article, we will see how to batch the above multiple operations into a single call.

Create SharePoint List

To create the SharePoint List we will be using the below the body in the batch call which will create a list with the title “Covid Medicine Inventory” and add columns “Medicine” and “Count”. We will be using the POST method against the endpoint – /sites/<site id>/lists

{
“id”: “1”,
“method”: “POST”,
“headers”: {
“Content-Type”: “application/json”
},
“url”: “/sites/bf7488ef-59ab-41a0-b3ff-6f6e8ee267f4/lists”,
“body”: {
“displayName”: “Covid Medicine Inventory”,
“columns”: [
{
“name”: “Medicine”,
“text”: {}
},
{
“name”: “Count”,
“number”: {}
}
],
“list”: {
“template”: “genericList”
}
}
}

Send Intimation mail

We also need to send an intimation mail to a user informing him about the completion of the List creation. But at the same time ensure that the mail is triggered only after list creation. To ensure the sequential execution we can use the dependsOn attribute and mention the ID of the action to ensure that it executes post the completion of the parent action. Within the body, we will mention the subject, email body and recipient and use the POST method against me/messages endpoint

“id”: “2”,
“dependsOn”: [
“1”
],
“method”: “POST”,
“url”: “me/messages”,
“headers”: {
“Content-Type”: “application/json”
},
“body”: {
“subject”: “List Created”,
“importance”: “Low”,
“body”: {
“contentType”: “HTML”,
“content”: “The List <b>Covid Medicine Inventory</b> has been created!”
},
“toRecipients”: [
{
“emailAddress”: {
“address”: “kspriyaranjan@gmail.com”
}
}
]
}

Rename the File in Document Library

As the next action, we will be renaming a file in the document library using the PATCH method against the /sites/<Site ID/drives/<Drive ID>/root:/filename

{
“id”: “3”,
“method”: “PATCH”,
“url”: “/sites/bf7488ef-59ab-41a0-b3ff-6f6e8ee267f4/drives/b!74h0v6tZoEGz_29ujuJn9K-7rrBWA4NAsxa5yVsgvaaH3_mjA8YpSoXnyF_Fqkj9/root:/document.docx”,
“headers”: {
“Content-Type”: “application/json”
},
“body”: {
“name”: “RenamedFileName.docx”
}
}

Remove the Permission from File

As the last action, we will be removing specific permission from the file using the DELETE method against the end point : https://graph.microsoft.com/v1.0/sites/<Site ID>/drives/<Drive ID>/items/<item id>/permissions/<Permission ID>

{
“id”: “4”,
“dependsOn”: [
“3”
],
“method”: “DELETE”,
“url”: “/sites/bf7488ef-59ab-41a0-b3ff-6f6e8ee267f4/drives/b!74h0v6tZoEGz_29ujuJn9K-7rrBWA4NAsxa5yVsgvaaH3_mjA8YpSoXnyF_Fqkj9/items/01KBMCOLS5OYBYG5T3YJD2Z75W3XZMCS35/permissions/Q29tbXVuaWNhdGlvbiBzaXRlIFZpc2l0b3Jz”
}

Test the batch

Let’s collate the batches together and use the endpoint https://graph.microsoft.com/v1.0/$batch to issue a POST request which will execute the batch in the order of the dependsOn property.

Graphical user interface, text, application

Description automatically generated

All 4 actions have succeeded and we have received an OK 200 code.

Graphical user interface, text, application, email

Description automatically generated Note: In case you are using dependsOn property ensure that all the batches except the Parent action have been assigned a dependsOn property, else you are likely to stumble upon the below error.

Graphical user interface, text, application, email

Description automatically generated

Summary

Thus we saw how to use Graph API to batch requests to reduce the turnaround time of multiple operations across multiple requests.

Related Articles

Author

Author

Priyaranjan KS is a Modern Workplace Architect primarily focused on developing and architecting solutions around Office 365,Power Platform and Azure.He is also a Microsoft Most Valuable Professional(MVP) and a Microsoft Certified Trainer(MCT)

Latest Articles