Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This tutorial teaches you how to create a custom Power Automate connector that calls Microsoft Graph.
Note
This tutorial assumes you have read the custom connector overview to understand custom connectors.
Prerequisites
- Administrator access to a Microsoft 365 tenant. If you don't have a Microsoft 365 tenant, you might qualify for one through the Microsoft 365 Developer Program; for details, see the FAQ. Alternatively, you can sign up for a one-month free trial or purchase a Microsoft 365 plan.
- Access to Microsoft Power Automate with a Premium license. See Power Automate licensing FAQ for details. If you don't have a Premium license, you can sign up for a 90-day trial.
Register an app in the Microsoft Entra admin center
Open a browser and navigate to the Microsoft Entra admin center and sign in using a Global administrator account.
Select Microsoft Entra ID in the left-hand navigation, expand Identity, expand Applications, then select App registrations.
Choose the New registration. Enter
MS Graph Batch App
in the Name field. In the Supported account types section, select Accounts in any organizational directory. Leave the Redirect URI section blank and choose Register.On the MS Graph Batch App page, copy the Application (client) ID. You need this value to configure your connector.
Choose API permissions under Manage. Choose Add a permission under API permissions.
In the Request API permissions page, choose the Microsoft Graph, then choose Delegated permissions. Search for
group
, then select the Group.ReadWrite.All delegated permission. Choose Add permissions at the bottom of the page.Choose the Certificates and secrets entry in the Manage section of the MS Graph Batch App page, then choose New client secret. Enter a description, choose a duration, and select Add.
Copy the value for the new secret. You need this value to configure your connector.
Important
This step is critical as the secret isn't accessible once you leave this page.
To enable management of other services accessible via the Microsoft Graph, including Teams properties, you would need to select the appropriate scopes to enable managing specific services. For example, to extend our solution to enable creating OneNote Notebooks or Planner plans, buckets and tasks you would need to add the required permission scopes for the relevant APIs.
Create the connector
Open a browser and navigate to Microsoft Power Automate. Sign in with your Microsoft 365 tenant administrator account. Choose Custom connectors on the left-hand side menu. If Custom connectors isn't present in the menu, select More, then Discover all.
On the Custom Connectors page, choose the New custom connector link in the top right, then select the Create from blank item in the drop-down menu.
Enter
MS Graph Batch Connector
in the Connector name text box. Choose Continue.On the connector configuration General page, fill in the fields as follows, then select the Security button to continue.
- Scheme: HTTPS
- Host:
graph.microsoft.com
- Base URL:
/
On the Security page, fill in the fields as follows, then select the Definition button to continue.
- Choose what authentication is implemented by your API:
OAuth 2.0
- Identity Provider:
Microsoft Entra ID
- Client id: the application ID from your app registration
- Client secret: the client secret from your app registration
- Login url:
https://login.microsoftonline.com
- Tenant ID:
common
- Resource URL:
https://graph.microsoft.com
(no trailing /) - Scope: Leave blank
- Choose what authentication is implemented by your API:
On the Definition page, select New Action and fill in the fields as follows.
- Summary:
Batch
- Description:
Make batch request with delegated permissions
- Operation ID:
Batch
- Visibility:
important
- Summary:
Select Import from sample under Request and fill in the fields as follows. Select Import to continue.
- Verb:
POST
- URL:
https://graph.microsoft.com/v1.0/$batch
- Headers: Leave blank
- Body:
{}
- Verb:
Select Create connector on the top-right.
After the connector is created, copy the generated Redirect URL from the Security tab.
Update your app registration in the Microsoft Entra admin center. Select Authentication on the left-hand side menu. Select Add a platform, then select Web. Enter the redirect URL copied from the previous step in the Redirect URIs, then select Configure.
Authorize the connector
The final configuration step to ensure the connector is ready for use is to authorize and test the custom connector to create a cached connection.
Important
The following steps require that you're logged in with administrator privileges.
In Microsoft Power Automate, choose Connections on the left-hand side menu. If Connections isn't present in the menu, select More. Select the New connection link.
Find your custom connector and complete the connection by selecting it, then choosing Create. Sign in with your Microsoft 365 tenant administrator's account.
When prompted for the requested permissions, check Consent on behalf of your organization and then choose Accept to authorize permissions.
After you authorize the permissions, a connection is created in Power Automate. The custom connector is now configured and enabled. There might be a delay in permissions being applied and available, but the connector is now configured.
Create a flow
In Microsoft Power Automate, choose My flows in the left-hand navigation. Choose New flow, then Instant cloud flow. Enter
Create Team
for Flow name, then select Manually trigger a flow under Choose how to trigger this flow. Select Create.Choose + under the Manually trigger a flow item. Under By connector, select Custom. Type
Batch
in the search box, then select Batch under MS Graph Batch Connector.In the properties window, change the name from Batch to
Batch POST-groups
.In the Advanced parameters dropdown, select body. Add the following code into the Body text box of the action.
{ "requests": [ { "url": "/groups", "method": "POST", "id": 1, "headers": { "Content-Type": "application/json" }, "body": { "description": "REPLACE", "displayName": "REPLACE", "groupTypes": ["Unified"], "mailEnabled": true, "mailNickname": "REPLACE", "securityEnabled": false } } ] }
Replace each
REPLACE
placeholder by selecting theName
value from the manual trigger from the Add dynamic content menu.Choose + under the Batch POST-groups item. Search for
delay
and add a Delay action and configure for 1 minute.Choose + under the Delay item, and add the Batch action from the custom connector.
In the properties window, change the name from Batch to
Batch PUT-team
.In the Advanced parameters dropdown, select body. Add the following code into the Body text box of the action.
{ "requests": [ { "id": 1, "url": "/groups/REPLACE/team", "method": "PUT", "headers": { "Content-Type": "application/json" }, "body": { "memberSettings": { "allowCreateUpdateChannels": true }, "messagingSettings": { "allowUserEditMessages": true, "allowUserDeleteMessages": true }, "funSettings": { "allowGiphy": true, "giphyContentRating": "strict" } } } ] }
Select the
REPLACE
placeholder, then select Expression in the dynamic content pane. Add the following formula into the Expression, then select Add.body('Batch_POST-groups').responses[0].body.id
This formula specifies that we want to use the group ID from the result of the first action.
Choose Save, then choose Test to execute the flow.
Tip
If you receive an error like
The template validation failed: 'The action(s) 'Batch_POST-groups' referenced by 'inputs' in action 'Batch_2' are not defined in the template'
, the expression is incorrect and likely references a flow action it can't find. Ensure that the action name you're referencing matches exactly.Choose the Manually action radio button and choose Test. Provide a name without spaces, and choose Run flow to create a Team.
Finally, choose Done to see the activity log. Once the flow completes, your Microsoft 365 Group and Team are configured. Select the Batch action items to view the results of the JSON Batch calls. The
outputs
of theBatch PUT-team
action should have a status code of 201 for a successful Team association.
Make multiple API calls in a single batch
The Flow you created in the previous section uses the $batch
API to make two individual requests to the Microsoft Graph. Calling the $batch
endpoint this way provides some benefit and flexibility, but the true power of the $batch
endpoint comes when executing multiple requests to Microsoft Graph in a single $batch
call. Extend the example of creating a Unified Group and associating a Team to include creating multiple default channels for the Team in a single $batch
request.
In Power Automate, select your Create Team flow, and choose Edit.
Choose + under the Batch PUT-team item, and add the Batch action from the custom connector.
In the properties window, change the name from Batch to
Batch PUT-team
.In the Advanced parameters dropdown, select body. Add the following code into the Body text box of the action.
{ "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." } } ] }
Notice the three requests use the dependsOn property to specify a sequence order, and each performs a POST request to create a new channel in the new Team.
Select each instance of the
REPLACE
placeholder, then select Expression in the dynamic content pane. Add the following formula into the Expression.body('Batch_PUT-team').responses[0].body.id
Save and Test the updated flow. Wait for the flow to complete, then verify that the Batch POST-channels action has a 201 HTTP status code.
Browse to Microsoft Teams and sign in with your Microsoft 365 tenant administrator account. Verify that the team you just created appears and includes the three channels created by the
$batch
request.
While the Batch POST-channels
action was implemented in this tutorial as a separate action, the calls to create the channels could be added as extra calls in the Batch PUT-team
action. This would create the Team and all Channels in a single batch call. Give that a try on your own.
Finally, remember that JSON Batching calls return an HTTP status code for each request. In a production process, you might want to combine post processing of the results with an Apply to each
action and validate each individual response has a 201 status code or compensate for any other status codes received.