{"id":5799,"date":"2021-01-26T06:00:50","date_gmt":"2021-01-26T14:00:50","guid":{"rendered":"https:\/\/officedevblogs.wpengine.com\/?p=5799"},"modified":"2021-11-17T12:44:11","modified_gmt":"2021-11-17T20:44:11","slug":"microsoft-graph-mailbag-batch-requests-with-power-automate-custom-connectors","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/microsoft-graph-mailbag-batch-requests-with-power-automate-custom-connectors\/","title":{"rendered":"Microsoft Graph Mailbag &#8211; Batch requests with Power Automate custom connectors"},"content":{"rendered":"<p>In this Microsoft Graph Mailbag post we will cover the benefits of building Power Automate custom connectors for Microsoft Graph. \u00a0This 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, <a href=\"https:\/\/docs.microsoft.com\/power-automate\/?WT.mc_id=m365-00000-aycabas\">Power Automate<\/a> 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.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-5842 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image9.jpg\" alt=\"\" width=\"574\" height=\"149\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image9.jpg 574w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image9-300x78.jpg 300w\" sizes=\"(max-width: 574px) 100vw, 574px\" \/><\/p>\n<p>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 <a href=\"https:\/\/docs.microsoft.com\/graph\/tutorials\/power-automate?WT.mc_id=m365-00000-aycabas\">Microsoft Graph Power Automate Tutorial<\/a> for step-by-step guidance.<\/p>\n<p>Please be sure to follow this blog series using <a href=\"https:\/\/aka.ms\/MSGraphMailbag\">https:\/\/aka.ms\/MSGraphMailbag<\/a> or with RSS using <a href=\"https:\/\/developer.microsoft.com\/graph\/blogs\/feed\/?tag=MSGraphMailbag\">https:\/\/developer.microsoft.com\/graph\/blogs\/feed\/?tag=MSGraphMailbag<\/a>.<\/p>\n<h2>Using built-in connectors vs. building your own custom connector<\/h2>\n<p>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.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-5835 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image1-1024x576.jpg\" alt=\"\" width=\"912\" height=\"509\" \/><\/p>\n<p>Addressing scenarios for calling Microsoft Graph directly, the HTTP connector in Microsoft Power Automate enables flexible integrations if you don\u2019t need to cache a user&#8217;s credentials. On the other hand, for the cases where you need to grant specific delegated permissions that require a user\u2019s 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.<\/p>\n<h2>Why Microsoft Graph JSON Batch?<\/h2>\n<p>One of the scenarios that requires creating a Microsoft Graph custom connector could be <strong>automating the process of team creation and configuration on Microsoft Teams<\/strong>. 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.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-5837 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image8-1024x459.jpg\" alt=\"\" width=\"821\" height=\"347\" \/><\/p>\n<p>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.<\/p>\n<p><strong>Quick introduction to Microsoft Graph JSON batching:<\/strong> 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:<\/p>\n<ul>\n<li>A picture of the user<\/li>\n<li>A list of joined teams on Microsoft Teams<\/li>\n<li>A list of calendar events<\/li>\n<\/ul>\n<p>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 <a href=\"https:\/\/docs.microsoft.com\/graph\/json-batching?WT.mc_id=m365-00000-aycabas\">Microsoft Graph JSON batching documentation<\/a>.<\/p>\n<h2>Test batching in Graph Explorer<\/h2>\n<p>Before creating our custom connector, let us test Microsoft Graph JSON batching in Graph Explorer to understand the concept in detail.<\/p>\n<p>Visit <a href=\"https:\/\/aka.ms\/ge\">https:\/\/aka.ms\/ge<\/a> and login with your <a href=\"https:\/\/developer.microsoft.com\/microsoft-365\/dev-program\">Microsoft 365 Developer Program<\/a> account. Search for \u201cbatch\u201d under Sample queries and select \u201cPerform parallel GETs\u201d.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-5838 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image2-1024x482.jpg\" alt=\"\" width=\"926\" height=\"436\" \/><\/p>\n<p>Replace Request Body with the following JSON and change GET to \u201cPOST\u201d as a request HTTP verb. Then, select \u201cRun query\u201d:<\/p>\n<pre class=\"\">{\n \u00a0\u00a0 \"requests\": [\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"url\": \"\/me\/photo\/$value\",\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"method\": \"GET\",\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"id\": \"1\"\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 },\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"url\": \"\/me\/joinedTeams\",\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"method\": \"GET\",\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"id\": \"2\"\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 },\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"url\": \"\/me\/events\",\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"method\": \"GET\",\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"id\": \"3\"\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0 ]\n}<\/pre>\n<p>Response should return user\u2019s picture, joined teams on Microsoft Teams, and all calendar events.<\/p>\n<h2>Choose the way you want to create your custom connector<\/h2>\n<p>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.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-5839 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image3.jpg\" alt=\"\" width=\"682\" height=\"524\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image3.jpg 834w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image3-300x230.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image3-768x589.jpg 768w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/p>\n<p>Any option you prefer to <a href=\"https:\/\/docs.microsoft.com\/graph\/tutorials\/power-automate?tutorial-step=2&amp;WT.mc_id=m365-00000-aycabas\">create your custom connector<\/a>, 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\u2019s credentials when we build our flow. More information is available in the <a href=\"https:\/\/docs.microsoft.com\/azure\/active-directory\/develop\/quickstart-register-app?WT.mc_id=m365-00000-aycabas\">QuickStart documentation for Azure Active Directory app registration<\/a>.<\/p>\n<p>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:<\/p>\n<ul>\n<li><strong>Verb:<\/strong> POST<\/li>\n<li><strong>URL:<\/strong> https:\/\/graph.microsoft.com\/v1.0\/$batch<\/li>\n<li><strong>Headers:<\/strong> [Leave blank]<\/li>\n<li><strong>Body:<\/strong> {}<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\" wp-image-5840 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image4.jpg\" alt=\"\" width=\"763\" height=\"497\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image4.jpg 700w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image4-300x195.jpg 300w\" sizes=\"(max-width: 763px) 100vw, 763px\" \/><\/p>\n<p><img decoding=\"async\" class=\" wp-image-5833 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image5.jpg\" alt=\"\" width=\"358\" height=\"519\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image5.jpg 500w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image5-207x300.jpg 207w\" sizes=\"(max-width: 358px) 100vw, 358px\" \/><\/p>\n<p>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, <strong>Data &gt; Connection<\/strong> section.<\/p>\n<p>In Microsoft Graph Power Automate tutorial, <a href=\"https:\/\/docs.microsoft.com\/graph\/tutorials\/power-automate?tutorial-step=2&amp;WT.mc_id=m365-00000-aycabas\">Create a custom connector for Microsoft Graph JSON Batch<\/a> and <a href=\"https:\/\/docs.microsoft.com\/graph\/tutorials\/power-automate?tutorial-step=3&amp;WT.mc_id=m365-00000-aycabas\">Authorize the connector<\/a> steps are explained in details.<\/p>\n<h2>Create a flow<\/h2>\n<p>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 <strong>Custom. <\/strong>Add your \u201cBatch\u201d custom connector action as a new step. \u00a0It will have an empty body.<\/p>\n<p>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.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-5834 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image6.jpg\" alt=\"\" width=\"539\" height=\"496\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image6.jpg 671w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image6-300x276.jpg 300w\" sizes=\"(max-width: 539px) 100vw, 539px\" \/><\/p>\n<p>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 <strong>Save<\/strong> and <strong>Test<\/strong> from top right. (Hint: you may run your joined teams query in the Graph Explorer and grab a team id from the query response.)<\/p>\n<pre class=\"\">{\n  \"requests\": [\n \u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0 \"id\": 1,\n \u00a0\u00a0\u00a0\u00a0 \"url\": \"\/teams\/REPLACE\/channels\",\n \u00a0\u00a0\u00a0\u00a0 \"headers\": {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Content-Type\": \"application\/json\"\n \u00a0\u00a0\u00a0\u00a0 },\n \u00a0\u00a0\u00a0\u00a0 \"method\": \"POST\",\n \u00a0\u00a0\u00a0\u00a0 \"body\": {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"displayName\": \"Marketing Collateral\",\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"description\": \"Marketing collateral and documentation.\"\n \u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0 },\n \u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0 \"id\": 2,\n \u00a0\u00a0\u00a0\u00a0 \"dependsOn\": [\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"1\"\n \u00a0\u00a0\u00a0\u00a0 ],\n \u00a0\u00a0\u00a0\u00a0 \"url\": \"\/teams\/REPLACE\/channels\",\n \u00a0\u00a0\u00a0\u00a0 \"headers\": {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Content-Type\": \"application\/json\"\n \u00a0\u00a0\u00a0\u00a0 },\n \u00a0\u00a0\u00a0\u00a0 \"method\": \"POST\",\n \u00a0\u00a0\u00a0\u00a0 \"body\": {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"displayName\": \"Vendor Contracts\",\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"description\": \"Vendor documents, contracts, agreements and schedules.\"\n \u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0 },\n \u00a0\u00a0 {\n \u00a0\u00a0\u00a0\u00a0 \"id\": 3,\n \u00a0\u00a0\u00a0\u00a0 \"dependsOn\": [\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"2\"\n \u00a0\u00a0\u00a0\u00a0 ],\n \u00a0\u00a0\u00a0\u00a0 \"url\": \"\/teams\/REPLACE\/channels\",\n \u00a0\u00a0\u00a0\u00a0 \"headers\": {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"Content-Type\": \"application\/json\"\n \u00a0\u00a0\u00a0\u00a0 },\n \u00a0\u00a0\u00a0\u00a0 \"method\": \"POST\",\n \u00a0\u00a0\u00a0\u00a0 \"body\": {\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"displayName\": \"General Client Agreements\",\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"description\": \"General Client documents and agreements.\"\n \u00a0\u00a0\u00a0\u00a0 }\n \u00a0\u00a0 }\n  ]\n}<\/pre>\n<p>After a successful flow trigger, you should immediately see newly generated channels under the related team on Microsoft Teams.<\/p>\n<p><img decoding=\"async\" class=\" wp-image-5836 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image7-1024x631.jpg\" alt=\"\" width=\"807\" height=\"497\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image7-1024x631.jpg 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image7-300x185.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image7-768x474.jpg 768w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2021\/01\/2021-01-25-Image7.jpg 1179w\" sizes=\"(max-width: 807px) 100vw, 807px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>If you would like to build Microsoft Graph JSON Batch custom connector and use it in a flow today, visit <a href=\"https:\/\/docs.microsoft.com\/graph\/tutorials\/power-automate?WT.mc_id=m365-00000-aycabas\">Microsoft Graph Power Automate tutorial<\/a> 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.<\/p>\n<p>Today\u2019s post was written by Ayca Bas, Cloud Advocate on the Microsoft Developer Relations team.\u00a0 You can follow Ayca on her <a href=\"https:\/\/quickbites.dev\/\">blog<\/a> or on Twitter <a href=\"https:\/\/twitter.com\/aycabs\">@Aycabs<\/a>.\u00a0 Join us for our next Microsoft Graph Mailbag post Feb 9, 2021.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Build Power Automate custom connectors to batch requests for Microsoft Graph.<\/p>\n","protected":false},"author":69075,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[21,17,65],"class_list":["post-5799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","tag-connectors","tag-msgraphmailbag","tag-power-automate"],"acf":[],"blog_post_summary":"<p>Build Power Automate custom connectors to batch requests for Microsoft Graph.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/5799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69075"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=5799"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/5799\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=5799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=5799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=5799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}