Introduction
Environment Variables are very useful when it comes to moving apps or flow from development and testing environment into a production environment. An Environment Variable is effectively a piece of data that stays static in the current environment but can change when deploying to a another environment.
One use case for this is a SharePoint Site or List. Lets say you have a SharePoint list that that tracks orders you wouldn’t want to use the same list in the app your developing in your development environment as your production environment, that’s where environment variables come in you can use an environment variable to store data like, site, list, folder, strings and use then across environments without implicating other environments.
Setting Up An Environment Variable
Prerequisite: Ensure you have at least two environments set up up first
Navigate to https://make.powerapps.com and go to solutions
Give the environment variable a Display Name (This can be changed later) and a ‘Name’. Select the data type you wish to use. For this example we are going to set up an environment variable for a SharePoint List, but first of all we will need to set up the Environment Variable for the SharePoint Site. Under ‘Data Type’ select ‘Data Source’. If not done so already you may be prompted to set up a new SharePoint connection.
In the settings under Parameter Type’ select ‘Site’ and then under ‘Current Site’ select the SharePoint Site you want to use for the development environment once you have selected the site click ‘Save’.
To Set up the SharePoint List Environment Variable follow the same steps including ‘Data Type’, this time under ‘Parameter Type’ select ‘List’, a option will appear to first select the SharePoint Site the SharePoint List appears in. Select the environment variable that you previously set up, and click Save.
Using Environment Variable In Power Automate Flow
Once an environment variable is created you will be able to use it inside a power automate flow. In this example we will use the environment variable to get items from the orders SharePoint List.
Using the ‘Get Items’ action we can use dynamic content to get items from the SharePoint list, instead of selecting a site from the drop down we can simply use the environment variable to define the site address, on the ‘Site Address’ select ‘Enter Custom Value’ and select the SharePoint site.
Please Note: Power automate flow must be in a solution for the environment variables to be visible in the dynamic content
We can do the same for the ‘List Name’…
The end result:
Using Environment Variable In Canvas App
We can also use Environment Variables in a Canvas Apps we can do this in a couple of ways.
Getting Data Source from Environment Variable
Once the environment Variable is selected, you will then need to select the data source, again select ‘Advanced’ tab and choose your environment variable for the data source.
The data source will now be added to the data section in your canvas app.
Showing Data from Environment Variables
When an Environment variable is created, there are actually stores inside a Dataverse Table. If we go https://make.powerapps.com and search ‘Environment’ you will see two Dataverse Tables related to the environment variables
- Environment Variable Definition – Stores the variable
- Environment Variable Value – Stores the value of each variable
Let’s say we want to show the Environment URL to ensure we are in the correct canvas app we can show this within the canvas app. To start add both of these tables into the app.
Next If we go into the dataverse table we can see under value is what we want to appear in the app.
If we go into the properties for ‘Environment Variable Definition’ column we can see this is a lookup to ‘If we go into the properties for ‘Environment Variable Definition’.
If we now go into ‘If we go into the properties for ‘Environment Variable Definition’ table we can now look for the ‘Schema Name’ of the variable, this is the ‘Name’ of the environment Variable when it was set up.
Now if we go into the Canvas App we should now be able to return the value of the SharePoint site by doing a lookup
LookUp('Environment Variable Values','Environment Variable Definition'.'Schema Name'="{VaribleSchemeaName}").Value
If we put the above code into a label (Replace {VaribleSchemeaName} with actual schema name of variable) we should now see that value appear within the app on the label we created
Using the environment variable across environments doesn’t just give us the ability to select data sources from one environment to another it can always allow us different colour of themes depending on the environment.
For example if we created an environment variable called ‘Environment Name’ and used the values (DEV, TEST, UAT, PROD) and set these in each environment.
Switch(LookUp('Environment Variable Values','Environment Variable Definition'.'Schema Name'="pt_EnvironmentName").Value,
"DEV",PowerAppsTheme,
"TEST",TeamsTheme,
"UAT",SharePointTheme,
"PROD",SharePointTheme
)