How to Get Environment name in Power Apps canvas apps

Hello everyone and welcome to my blog. I am back with another blog on canvas apps and today I will discuss on how to get the current environment name in Power Apps canvas apps.

While we will focus on Canvas apps, the article shall help you explore the below scenarios as well.

  • You want to get the current environment details inside Power Apps
  • You want to fetch all environment details you have access to inside a Power Automate or Power Apps
  • You want to fetch the current environment details in Power Automate.

As of the time of writing this blog, Canvas apps does not have a mechanism to get the current environment details. We have to take the help of Power Automate flow.

Since the flow shall be called from inside Canvas apps, I create a flow with the Power Apps connector.

In the next step, I use ‘Initialize Variable’ action to get the current environment id.

Note the expression used to get the current environment id.

workflow().tags.environmentName

It’s a process variable which give the current environment id where the power automate flow is executing.

Then I use another Initialize Variable step to store the current environment display name. I am going to populate it in a later step.

Now the most important part – the step to get the environment details. For this I used the Power Apps for Makers connector and Get Environment action.

This output of the action give me the list of all environments I have access to. Below is the sample JSON response for one environment.

  {
      "id": "/providers/Microsoft.PowerApps/environments/941bc4df-0a0f-eeee-ac31-f68c5e2661d2",
      "name": "941bc4df-0a0f-eeee-ac31-f68c5e2661d2",
      "location": "india",
      "type": "Microsoft.PowerApps/environments",
      "properties": {
         "azureRegionHint": "centralindia",
         "displayName": "Debajit Dutta's Environment (org611ca745)",
         "createdTime": "2022-09-21T12:11:07.5444868Z",
         "createdBy": {
            "id": "SYSTEM",
            "displayName": "SYSTEM",
            "type": "NotSpecified"
         },
         "provisioningState": "Succeeded",
         "creationType": "Developer",
         "environmentSku": "Developer",
         "environmentType": "Production",
         "isDefault": false,
         "runtimeEndpoints": {
            "microsoft.BusinessAppPlatform": "https://india.api.bap.microsoft.com",
            "microsoft.CommonDataModel": "https://india.api.cds.microsoft.com",
            "microsoft.PowerApps": "https://india.api.powerapps.com",
            "microsoft.PowerAppsAdvisor": "https://india.api.advisor.powerapps.com",
            "microsoft.PowerVirtualAgents": "https://powervamg.in-il101.gateway.prod.island.powerapps.com",
            "microsoft.ApiManagement": "https://management.INDIA.azure-apihub.net",
            "microsoft.Flow": "https://india.api.flow.microsoft.com"
         },
         "linkedEnvironmentMetadata": {
            "type": "Dynamics365Instance",
            "resourceId": "bdc295e1-013b-4966-9b24-d88574310506",
            "friendlyName": "Debajit Dutta's Environment",
            "uniqueName": "unqbdc295e1013b49669b24d88574310",
            "domainName": "org611ca745",
            "version": "9.2.22103.00172",
            "instanceUrl": "https://org611ca745.crm8.dynamics.com/",
            "instanceApiUrl": "https://org611ca745.api.crm8.dynamics.com",
            "baseLanguage": 1033,
            "instanceState": "Ready",
            "createdTime": "2022-09-21T12:11:21.737Z",
            "modifiedTime": "2022-11-04T12:32:28.8948027Z",
            "hostNameSuffix": "crm8.dynamics.com",
            "bapSolutionId": "00000001-0000-0000-0001-00000000009b",
            "creationTemplates": [
               "D365_CDS"
            ],
            "webApiVersion": "v9.0",
            "platformSku": "Standard"
         },
         "retentionPeriod": "P7D",
         "lifecycleAuthority": "Environment",
         "states": {
            "management": {
               "id": "NotSpecified"
            },
            "runtime": {
               "runtimeReasonCode": "NotSpecified",
               "requestedBy": {
                  "displayName": "SYSTEM",
                  "type": "NotSpecified"
               },
               "id": "Enabled"
            }
         },
         "updateCadence": {
            "id": "Frequent"
         },
         "connectedGroups": [],
         "protectionStatus": {
            "keyManagedBy": "Microsoft"
         },
         "trialScenarioType": "None",
         "cluster": {
            "category": "Prod",
            "number": "101",
            "uriSuffix": "in-il101.gateway.prod.island",
            "geoShortName": "IN",
            "environment": "Prod"
         },
         "governanceConfiguration": {
            "protectionLevel": "Basic"
         }
      }
   }

Observe the name property highlighted in bold in the above JSON. That is the property we need to compare against. If you observe the JSON carefully, any property or information related to the environment is available in the JSON output.

Next and quite obvious is to use the IF condition to compare with the environment ID and find out the current environment details.

As you can see, I am comparing with the currentEnvId variable from earlier step to get the current environment details. Please note that I am using the contains operator instead of equals. This is because the name property for the Default environment come with the value – “Default – <environment id>.

And that’s it. The final step is to pass the value back to Power Apps. For this I use the ‘Respond to a PowerApp or Flow’ connector and set the return value to the environment name variable captured in the previous step.

That’s it. We are done from the flow side. The next step is to invoke this Power Automate flow from canvas app. I add the Power Automate flow to my Canvas app.

And then invoke the same on a button ‘On Select‘ event.

That’s pretty much. The output is stored inside a variable ‘currEnvName‘ which you can use it anywhere inside your app.

I hope you liked this post. If this post has helped you, you can buy me a coffee. Links on right panel.

You will also like the below posts.

Debajit Dutta
Business Solutions MVP