Get Environment Variables in Canvas app

The best practice while making any Power Platform project, is to use Environment variables. Let’s see how to get these variables into your Canvas App, with and without a premium Dataverse licence.

  1. What’s an Environment variable and why should I use it?
  2. Where is it stored? How does it work behind the scenes?
  3. If Environment variables is a Datasource type
  4. If Environment variables are from another type (such as string or int)
    1. Get my variables in Canvas app with a Dataverse Licence
    2. Get my variables in Canvas app without a Dataverse Licence

1. What’s an Environment variable and why should I use it?

An Environment variable is, as it speak for itself, a variable which can be used and set in your environment. It can be used through all the tools in Power Platform. Its value can be changed from an environment to another.

2. Where is it stored? How does it work behind the scenes?

That’s an important part, you need to understand how it works behind the scenes to easily understand how to get your environment variable value.

We are going to talk about Dataverse, and if you don’t have a Licence, don’t worry, it will be helpful to understand how it works for you too. Everything is stored into Dataverse. You have a table which store your environment variable definition, such as name or description, and you have another table which store the value. There are linked with a relation 1 from Variable Definition to Many from Variable Value

The main idea is going to get these values, regarding a definition.

3. If Environment variables is a Datasource type

This is a common scenario, and if you have or haven’t a Dataverse Licence, it works the same way. We are going to use SharePoint as example. So you are going to first select your SP Site, then your SP List from the SP Site environment variable. In this scenario, while deploying your solution from an environment to another, you are going to be able to switch for another site (from Dev to Prod for example).

Then in Canavas, you juste have to use the variable as Datasource. Choose SharePoint, while selecting a site, go into advanced to get your Environment Variable, then while choosing a List, go to Advanced to get your Environment Variable.

4.If Environment variables are from another type (such as string or int)

For the two following part, I am going to use a string as Environment variable which contains “Hello World”.

4.1. Get my variables in Canvas app with a Dataverse Licence

As mentioned above everything is stored into two variables, start with adding these two dataverse tables into your Canavas datasources.

Then you just need to do a lookup as following:

LookUp(
    'Environment Variable Values',
    'Environment Variable Definition'.'Schema Name' = "[Environment Variable Column Schema Name]",
    Value
)

If you add it into App.Onstart properties:

Or even better, if you add it into App.Formaulas properties:

4.2. Get my variables in Canvas app without a Dataverse Licence

Even if it is in Dataverse table, don’t forget you can get them into Power Automate without any Dataverse Licence.

The trick is to use a Power Automate, triggered from your Canavas App, which will return your Environment Variables.

Then in your canvas call, your flow (I have added my code into the OnVisible properties of my screen):