Power Apps – ‘Environment Variables’ and their usage in Canvas Apps
Lets take a scenario, where you would want to pass a 3rd party API URL to your plug-in which changes every quarter. Couple of options to pass the URL to plug-in are either pass the URL as ‘Secured/Unsecured parameters’ or Create an entity and store the API URL and fetch from plug-in using Query Expression.
Using ‘Environment variables’, which is Power Apps preview feature currently, we can configure the input parameters allow management of data separately compared to hard-coding values.
Up on creating ‘Environment variable’, a new record gets added to both OOB ‘Environment Variable Definition’ entity and ‘Environment Variable Values’ entities.
‘Environment Variable Values’ is child to the ‘Environment Variable Definition’ entity.
We can refer records from these OOB entities in plug-ins or Canvas Apps or Power Automate just like any other entities.
In this article, lets see how to create ‘Environment Variables’ and use them in Canvas App.
Prerequisites:
- Subscribe to 30 days trial and get Office 365 account.
- Connect to Power Apps maker portal using Office 365 account.
Create a new ‘Environment variable’:
- From Power Apps maker portal go to ‘Solutions’ tab.
- Either create a new solution/Open Existing solution.
- From ribbon, click on New -> Environment variable
- In the ‘New environment variable’ pane, provide ‘Display Name’ and choose ‘Data Type’.
- Provide either ‘Default Value’ or ‘Current Value’
- Save. I’ve created a ‘Text’ variable with an environment URL as ‘Current Value’.
- I’ve configured a Model driven app with ‘Environment Variable Definition’ entity added to the ‘Site Map’ to check the created ‘Environment variable’ record.
- Open the Model driven app and go to ‘Environment Variable Definition’ and you will see the record.
Using ‘Environment variable’ in Canvas app:
As the ‘Environment variable’ stores in ‘Environment Variable Definition’ and ‘Environment Variable Values’ entities, we can add these entities from ‘Data sources’.
- Create a new Canvas App.
- Add ‘Environment Variable Definitions’ and ‘Environment Variable Values’ entities as Data sources.
- Add a Label to display the ‘Environment variable’ value.
- Use ‘LookUp’ function to get the specific ‘Environment variable’ filtered by ‘Display Name’.
- LookUp(‘Environment Variable Values’,’Environment Variable Definition’.’Display Name’=”AppURL”).Value
- In the above formula, ‘AppURL’ is my ‘Environment Variable’ display name.
- The value would be fetched and set to the Label text.
Managing ‘Environment variables’:
- Environment variables can be created and managed through the modern solution interface or by using code.
- A separate JSON file is created within your solution package for the values, which can also be managed in source control and modified in a build pipeline.
- When you export the solution, a json file ‘environment_variable_values’ will be available.
- Open the file and the ‘Environment Variables’ render as json records.
- Export to and import from Excel is also supported.
🙂
Great article! Environment variables are an essential feature for managing data separately compared to hard-coding values, and it’s good to see how to create and use them in a Canvas App.
https://imperiumdynamics.com/power-apps