Use the Microsoft Entra tenant ID in Power Automate Flow
While working on a Power Automate Flow to automatically assign available Dynamics 365 F&O licenses, some actions required the Microsoft Entra tenant ID. In this post, you can read how I automated the tenant ID retrieval using a flow.
Use case
For several actions in connectors, you might need a tenant ID. In my example, I need to use the tenant ID while interacting with Microsoft Graph. In an HTTP request, one of the details to be provided is the tenant ID. When this flow is a customization for a single customer, you can decide to manually find the tenant ID and make it part of the URI. I recommend using a variable for the tenant ID, especially when it needs to be used multiple times in a flow.
In case you need to build a solution that will be distributed to multiple organizations, it is recommended to use a variable for the tenant ID, which prevents searching all details in a Power Automate flow, and replace the tenant ID values with the variable.
In the screenshot below, you can see how I added the Tenant ID as a variable in a flow and consumed the variable in an HTTP request action. This is a simplified overview of what I’m using in the full flow.

Manage variable values
Depending on the type of variable and from where to retrieve the value, you can set values manually in the variable itself, or use other storage options, like environment variables or Azure Key Vault. In the screenshot above, you can see that Azure Key Vault is used to store and retrieve secure data for this flow. In this case, the values depend on client-managed Azure configuration details, and I found a secure place without having the values visible for all users in an organization.
The tenant ID is a fixed value and belongs to the organization. When Microsoft Entra ID is set up, the domain is linked to a tenant. You can use the next Microsoft learn article to find the tenant ID: How to find your tenant ID – Microsoft Entra | Microsoft Learn
There are more options available to find the tenant ID. I wanted to go a step further and automatically retrieve the tenant ID while running the flow, so the value of this variable is not to be manually managed when the flow will be deployed.
While searching within documentation and on the internet, I learned that using the Microsoft Graph API to retrieve details from the organization is not working without an access token. In the request above to get an access token, I need the tenant ID. This feels like the chicken or the egg causality dilemma. There are other options available. E.g., I had seen an option to use the List teams action from the Microsoft Teams connector. I’m not intending to use Teams in my flow, so I would also not want to retrieve the tenant ID using connectors that do not have another purpose in the flow.
By searching further, I found out that the tenant ID is used in the trigger headers of a manually triggered flow, see the screenshot below.

Note that other triggers, like those from automated or scheduled flows, don’t have this trigger output value.
You can retrieve the value of the trigger output using the next Power Fx formula.
triggerOutputs()?['headers']['x-ms-tenant-id']
In your flow, use the option Insert expression and provide the formula as shared above. After you save the flow, it will be highlighted with the Manually trigger a flow icon.

Parent and child flows
In case you call a child flow, despite the child flow having a manual trigger, the trigger headers do not have the tenant ID as output. In case the parent flow is also manually triggered, you can retrieve the tenant ID in the parent flow and pass it to the child flow. The child flow then should have an input parameter for the tenant ID.

Imagine, the child flow should also run stand-alone, you can initialize the TenantID variable using a Power Fx formula:
if(empty(triggerBody()?['text_1']),triggerOutputs()?['headers']['x-ms-tenant-id'],triggerBody()?['text_1'])
In this example, text_1 is the code name for the Tenant ID input variable.

In case you are using a scheduled or automated flow, you will need to consider if you want to use an alternative, like List teams, or manually update the tenant ID in a variable.
I do hope you liked this post and will add value for you in your daily work as a professional. If you have related questions or feedback, don’t hesitate to use the Comment feature below.
That’s all for now. Till next time!












Leave a Reply
Want to join the discussion?Feel free to contribute!