Microsoft 365 Microsoft Graph

Microsoft Graph Mailbag – Batch requests with Power Automate custom connectors

In this Microsoft Graph Mailbag post we will cover the benefits of building Power Automate custom connectors for Microsoft Graph.  This includes details of Microsoft Graph JSON Batch, ways to create custom connectors, and how to use them in the process automation flows. For those unfamiliar, Power Automate is a no-code/low-code platform that provides us everything about automation for manual tasks. You can leverage Power Automate using built-in connectors or you can build your own custom connector to use in flows.

If you are interested in creating a Microsoft Graph JSON Batch Custom Connector for Power Automate today, there is a great tutorial available in Microsoft Graph Docs. Visit Microsoft Graph Power Automate Tutorial for step-by-step guidance.

Please be sure to follow this blog series using https://aka.ms/MSGraphMailbag or with RSS using https://developer.microsoft.com/graph/blogs/feed/?tag=MSGraphMailbag.

Using built-in connectors vs. building your own custom connector

There are many out of the box Power Automate connectors that use the Microsoft Graph to communicate with specific endpoints of Microsoft products. However, there is no built-in connector that communicates directly with the Microsoft Graph to cover the entire API, so that some scenarios may require calling the Microsoft Graph directly from Power Automate flows.

Addressing scenarios for calling Microsoft Graph directly, the HTTP connector in Microsoft Power Automate enables flexible integrations if you don’t need to cache a user’s credentials. On the other hand, for the cases where you need to grant specific delegated permissions that require a user’s credentials, a custom connector can be created to provide a wrapper around the Microsoft Graph API and enable consuming the API with delegated permissions. Another benefit of building a custom connector is to be able to leverage and reuse the same connector in various Power Automate flows for different scenarios.

Why Microsoft Graph JSON Batch?

One of the scenarios that requires creating a Microsoft Graph custom connector could be automating the process of team creation and configuration on Microsoft Teams. For this scenario, we can create a custom connector that consumes Microsoft Graph Teams APIs to create a team. Additionally, we would like to add members and to create channels, then we will need to create new custom connectors for these actions.

The more practical approach would be creating a custom connector for Microsoft Graph JSON Batch endpoint to enable multiple Microsoft Graph calls in Power Automate flows, so that we can reuse our custom connector in different Microsoft Graph related scenarios.

Quick introduction to Microsoft Graph JSON batching: JSON batching let us combine multiple requests into a single JSON object, so we can make several requests in one HTTP call. For example, a client might want to compose a view of unrelated data such as:

  • A picture of the user
  • A list of joined teams on Microsoft Teams
  • A list of calendar events

Combining these three individual requests into a single batch request using our JSON Batch custom connector on Power Automate will bring us all the information we need. For more details, check out the Microsoft Graph JSON batching documentation.

Test batching in Graph Explorer

Before creating our custom connector, let us test Microsoft Graph JSON batching in Graph Explorer to understand the concept in detail.

Visit https://aka.ms/ge and login with your Microsoft 365 Developer Program account. Search for “batch” under Sample queries and select “Perform parallel GETs”.

Replace Request Body with the following JSON and change GET to “POST” as a request HTTP verb. Then, select “Run query”:

{
    "requests": [
        {
            "url": "/me/photo/$value",
            "method": "GET",
            "id": "1"
        },
        {
            "url": "/me/joinedTeams",
            "method": "GET",
            "id": "2"
        },
        {
            "url": "/me/events",
            "method": "GET",
            "id": "3"
        }
    ]
}

Response should return user’s picture, joined teams on Microsoft Teams, and all calendar events.

Choose the way you want to create your custom connector

There are multiple ways to create a new custom connector in Power Automate. If you enjoy the step-by-step UI guidance, you may create your custom connector from a blank template. You may also import your custom connector for Microsoft Graph as an OpenAPI file/URL, a Postman collection, or from GitHub.

Any option you prefer to create your custom connector, you will get the flexibility of configuring your preferred authentication type for your API. For any custom connector consuming Microsoft Graph, we will require setting up 0Auth 2.0 as an authentication type and Azure Active Directory as an identity provider. Fill in this step with the credentials from a registered app in Azure Active Directory to enable caching a user’s credentials when we build our flow. More information is available in the QuickStart documentation for Azure Active Directory app registration.

For Microsoft Graph JSON Batch API scenario, you will need to create a new action for $batch in the custom connector and import from sample for generating the request by filling the fields as follows:

  • Verb: POST
  • URL: https://graph.microsoft.com/v1.0/$batch
  • Headers: [Leave blank]
  • Body: {}

Our final configuration will be authorizing and testing the custom connector to make sure that the connector is ready for use. You may complete this step in Microsoft Power Automate, Data > Connection section.

In Microsoft Graph Power Automate tutorial, Create a custom connector for Microsoft Graph JSON Batch and Authorize the connector steps are explained in details.

Create a flow

Your custom connector is ready to use in a Power Automate flow. If you create a flow and try to add a new step, you should be able to see your Microsoft Graph Batch custom connector under Custom. Add your “Batch” custom connector action as a new step.  It will have an empty body.

In this case, you may run any Microsoft Graph requests in the body of your Batch custom connector. Similarly, as you tried out in the Graph Explorer, the same request can be used in this step.

As an example, you may test your flow and connector by running the following request that creates three channels under an existing team on Microsoft Teams. Change REPLACE placeholder with an existing team id on Microsoft Teams, then Save and Test from top right. (Hint: you may run your joined teams query in the Graph Explorer and grab a team id from the query response.)

{
  "requests": [
    {
      "id": 1,
      "url": "/teams/REPLACE/channels",
      "headers": {
        "Content-Type": "application/json"
      },
      "method": "POST",
      "body": {
        "displayName": "Marketing Collateral",
        "description": "Marketing collateral and documentation."
      }
    },
    {
      "id": 2,
      "dependsOn": [
        "1"
      ],
      "url": "/teams/REPLACE/channels",
      "headers": {
        "Content-Type": "application/json"
      },
      "method": "POST",
      "body": {
        "displayName": "Vendor Contracts",
        "description": "Vendor documents, contracts, agreements and schedules."
      }
    },
    {
      "id": 3,
      "dependsOn": [
        "2"
      ],
      "url": "/teams/REPLACE/channels",
      "headers": {
        "Content-Type": "application/json"
      },
      "method": "POST",
      "body": {
        "displayName": "General Client Agreements",
        "description": "General Client documents and agreements."
      }
    }
  ]
}

After a successful flow trigger, you should immediately see newly generated channels under the related team on Microsoft Teams.

Conclusion

If you would like to build Microsoft Graph JSON Batch custom connector and use it in a flow today, visit Microsoft Graph Power Automate tutorial that walks you through the process of building Microsoft Graph JSON Batch connector, testing it in a complete flow in Power Automate, and creating a team with channels on Microsoft Teams.

Today’s post was written by Ayca Bas, Cloud Advocate on the Microsoft Developer Relations team.  You can follow Ayca on her blog or on Twitter @Aycabs.  Join us for our next Microsoft Graph Mailbag post Feb 9, 2021.

filter-icon