Using PowerApps Environment Variables

If you paid close attention to the entities in your environments you might have noticed that environmentvariabledefinitions and environmentvariablevalues have recently showed up in all CDS environment databases (Dynamics 365 apps included). You can also look at your Solution History and probably see a solutions called EnvironmentVariables and EnvironmentVariablesAnchor. If you have EnvironmentVariables 1.0.1.9 then you have the preview bits!

To use and see the Environment Variables as a component in the modern solution explorer you actually need to use the preview version of make.powerapps.com which is available at make.preview.powerapps.com. From there you should see it as a component you can create and add existing environment variables.

make.preview.powerapps.com New Component

When you create environment variables it is actually creating entity record data in the environmentvariablesdefinitions entity, if you add a value it adds to the environmentvariablesvalues entity related to the definition. You can only add 1 value per definition even though it is a 1:N relationship. The UI as well as plugins will block you from creating additional values.

How this data ends up in solutions is within the the customizations.xml with an <EnvironmentVariables></EnvironmentVariables> section and your values actually end up as environment_variable_values.json.

customizations.xml

<EnvironmentVariables>
    <Definition IntroducedVersion="1.0.0.0">
      <Name>demo_TestingVariable</Name>
      <IsRequired>0</IsRequired>
      <Type>100000000</Type>
      <DefaultValue>testnew</DefaultValue>
      <DisplayName>
        <LocalizedNames>
          <LocalizedName description="Testing Variable" languagecode="1033" />
        </LocalizedNames>
      </DisplayName>
      <Description>
        <LocalizedNames>
          <LocalizedName description="This is a testing variable" languagecode="1033" />
        </LocalizedNames>
      </Description>
    </Definition>
  </EnvironmentVariables>

environment_variables_values.json

{
  "EnvironmentVariableValues": [
    {
      "EnvironmentVariableDefinition.SchemaName": "demo_TestingVariable",
      "Value": "newtestvalue"
    }
  ]
}

So how do you go about using environment variables in your applications…It is just entity record data like any other entity, so you can query for it within plugins, canvas apps, flow, PCFs, etc. just as you would other entities. You will need to follow the logic pattern though of if value there is a value configured then use it if not then use default value found on the definition.

ALM Story with Environment Variables

Environment Variables are there to help solve the challenge of data that needs to be different for each environment, very much like a parameter. The most common pattern for this a DEV, TEST, PROD environment scenario but this is also going to be a very useful component for ISV based solutions where they deliver something that expects to talk to your version of that element and therefore you need to input your details.

For ISV patterns it is likely once installed in your environment you will go about creating a value for an existing environment variable. The value will override the default value found on the definition and away you go.

For the ALM pattern of DEV, TEST, PROD, etc. you probably want to manipulate the values as they are packaged into solutions. What is cool is you can use the existing Solution Packager Mapping to map your environment variables values file so that when you pack your solution you can pull the json file from another location and that location could be dependent on the environment you are working with therefore including the values for that environment.

Example solution packager mapping file for environment variables:

<?xml version="1.0" encoding="utf-8"?>
<Mapping>
  <FileToFile map="EnvironmentVariables\environment_variable_values.json" to="..\mapfiles\environment_variable_values_dev.json" />
</Mapping>

Example solution packager command with mapping file reference:

.\SolutionPackager.exe /action:Pack /zipfile:Solution.zip /folder:'SolutionFolder' /packagetype:Unmanaged /map:'Solution-dev.mappings.xml'

PowerApps Build Tools for Azure DevOps Pipelines Pack and Unpack commands currently do not take a mapping argument but the Dynamics 365 Build Tools from Wael do! So if you wanted to use the mapping method of transformation on there then look at his tools currently.

You could also do this with other PowerShell commands to manipulate the JSON before it is packed if required. I would advise sticking with the Solution Packager Mapping though as that is supported by the SDK itself.

Check out the preview documentation for more details.

NOTE: There is an issue with solution packager currently where it won’t unpack environment variables. I would expect a fix or update to CoreTools will resolve this soon. Just needs to catch-up to the preview release.

You will need Core Assemblies SDK 9.0.2.20+ to ensure support for environment variables with solution packager.
https://www.nuget.org/packages/Microsoft.CrmSdk.CoreAssemblies/9.0.2.20