Have you ever fancied your own trigger or action that you can call from Power Automate? This very same connector can be used in PowerApps too or you can build one specific for your Logic App. I am going to show you how I built a custom connector for the Graph API. The API call in question will allow me to post an attachment to Teams chat. This is currently not supported by the native Teams actions in Power Automate. I also have an article on how the flow is built here.
If you want to read up on Custom Connectors, more details are available from Microsoft here.
My custom connector is based on the Graph API. There is a very useful “Graph Explorer” tool that will allow you to explore the API capabilities. You can read up on this in more detail here.
Where to start?
First of all, this is the first time I have built a connector, albeit my requirement is such that I need to pass parameters into the URL, both Team and Channel GUIDs and so has an element of complexity. I have explored the Custom Connector functionality and got my solution working. This is not a one solution fits all but an example based on my scenario and experience. I will therefore show you how I built my connector for Graph API.
To get started, I first experimented with the Graph API via the Explorer. The Graph API is a treasure trove of functionality that goes well beyond the standard actions of Flow. You can browse and test most scenarios from the comfort of this screen. You could use an API for sending text messages to an SMS gateway. Or maybe talk to an IOT device? When you have chosen the Graph API action you wish to implement, make a note of the permissions required (under modified permissions or via Graph API guidance notes) as you will need to enable these later.
The Custom Connector
To get started with your Graph API connector you can go via Power Apps or Power Automate home pages. From the left-hand navigation bar, expand data and select Custom Connectors. You can see from my screenshot below I have a few sample connectors. My latest attempt was GraphNew. To create a new connector, select New Custom Connector.
The first stage of creating the connector are the general settings. Choosing a meaningful name, uploading an image that will be visible across the platform, a description and then the all importand host. I know that my host is graph.microsoft.com. Because all api actions stem from this root API, the base URL is simply /. This allows me to extend this connector with multiple actions in the future.
Security
Moving onto security (bottom right), this is where you must provide the authentication details to connect to the API. In my situation, I have set up an App registration on Azure. This allows the API to authenticate and speak to my tenant. If you do not have access to Azure Portal, you will have to ask your administrator to set this up for you. When I first started my learning on Custom Connectors, I followed the create a batch custom connector tutorial from Microsoft. This will demonstrate how to set up the App Registration. Once set up, you must enable the API permissions for the Application or Delegated user.
To complete the security settings, we must choose the method (and in this case OAuth 2.0). The client ID, client secret, and tenant ID are retrieved from your App registration details on Azure.
Login URL is https://login.windows.net.
Resource URL is https://graph.microsoft.com
Make sure you ask your Azure administrator to add the redirect URL to the Redirect URIs on the Authentication tab of the App Registration as well as the API Permissions for the Graph API for the Application or Delegated user. In my case I needed the delegated Group.ReadWrite.All permission.
Definition
Now that the authentication is out of the way, we must define things like the body, header, and parameters for our action or trigger. In this case, I am defining an action for my Power Automate Flow.
Below, I have chosen to create a new Action “PostToChannel”. This will enable me to post a message to the channel of my choosing as the logged-on user. After selecting New Action, I filled out the necessary names and descriptions for the new action. By marking the visibitily as important, the action will be visible to me when I search for flow actions.
The next stage for me was to import a sample request. I grabbed this from the Graph Explorer request body. My particular action is a post, the URL includes some dynamic values, team and channel GUIDs. Note that I have included variables in the sample URL in curly brackets { }
At this stage, importing the payload was enough to get the connector up and running. Path parameters were created and I was able to test the custom connector. If you would like to build the same connector, you can use the following sample JSON Body. This was copied from the Graph API guidance here.
{ "body": { "contentType": "html", "content": "Here's the latest budget. <attachment id=\"153fa47d-18c9-4179-be08-9879815a9f90\"></attachment>" }, "attachments": [ { "id": "153fa47d-18c9-4179-be08-9879815a9f90", "contentType": "reference", "contentUrl": "https://m365x987948.sharepoint.com/sites/test/Shared%20Documents/General/test%20doc.docx", "name": "Budget.docx" } ] }
There was no need to change the default response or validation. My resulting import looked like follows:
Testing
If not already done so, you want to press update connector. The last thing you want to do now is close your browser and lose your hard work! Moving on to testing, in my case due to using Graph API and OAuth you will need to provide Azure AD credentials for your connection. You can then supply the various parameters and even paste in your JSON body from the Graph Explorer to test. All being well you will get the magic 201 response!
The Action in Power Automate
With the connector now tested and working, you can start building a flow to use this new action! Jump on to Power Automate and select New Step. Select the Custom Connectors and you will see your new connecter and various actions, in my case PostToChannel.
Selecting this action enables you to complete your action parameters just like any other out of the box action. We’ve just built our first custom connector using the Graph API and I will show you how I am going to use it in my next post!
Hello,
I have created the Custom Connector but I don’t see how you get the attachement ID of the created file.
Could you please shed a light on that matter.
Kind regards,
Pascal
Hello,
I have read that the attachment ID can be the same for every request. So when I now test I see the post and the attachment, but :
1. when I click on the attachment I get an “Uknown error” or a “You don’t have access to this file”.
2. when I add the link to attachment as parameter it says that the extension of the contenturl is different from the name with extension. Which is true because the link to the extension has some extra information to it.
How can I fix this?
Kind regards,
How to do lookup in excel using power automate for large data set