Use environment variable to store your configuration data in Dynamics 365/ CDS – A complete guide.

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com

  • What is the best way to store Configuration value/ global variables in Dynamics 365/ CDS?
  • Can I move my configuration value across environments using Solutions in CDS/ Dynamics 365?
  • Do I need an entity to store configuration values for my environment?
  • Do I need to set-up user security to set access global variables for my environment?
  • How can I change configuration/ global variables for my environment dynamically with minimum effort?

If you are having any of the above questions, you are on the right blog. In this blog, I am going to touch base on all these topics. In this blog I am going to introduce the concept of environment variable, the preview release of which dates back almost a year. But surprisingly till date, I still find usage of a separate entity to store environment variables. So let’s get this concept once and for all. Please note this feature is still in preview.

How do I configure environment variable?

The best way to create a environment variable is using solutions in CDS/ Dynamics 365. From your solution, use the Environment variable to create a new environment variable.

environment variable in CDS, Dynamics 365

Environment variable can be of the following types

  • Decimal
  • JSON
  • Two Options
  • Text
environment variable in CDS, Dynamics 365

Can I have multiple values for a single environment variable?

Microsoft have a vision to introduce the capability of multiple values for an environment variable in a future release. As of the current preview release, there is support for the following:

  • Default value – This is not a required field. However you can set this value when it does not need to change across multiple environments.
  • Current value – This is also known as override value. Again it’s an option field but you can set this value to override the default value in your current environment. Remove the value from your solution if you don’t want to use it in the next environment.

While the default value is stored in entity called “environmentvariabledefinition”, the actual variable is stored inside an entity “environmentvariablevalue”. Scroll below to find out the code to fetch the default value and current value for an environment variable.

environment variable in CDS, Dynamics 365

Is the environment variable controlled by Security?

The environment variables are stored in OOB entities – environmentvariabledefinition and environmentvariablevalue . They are user or team owned entities. Any user accessing the environment variables should have appropriate access to the entity. You can provide permission to this entity using security role.

Open any security role and navigate to Custom entities tab as highlighted in the screenshot below. Look for “Environment Variable Definition” & “Environment Variable Value” and provide appropriate permission.

environment variable in CDS, Dynamics 365

How can I move my environment variables across environment?

Environment variables are included as part of solutions. Just like with any other CDS solutions, you can export environment variables from one environment to another using solutions.

How to query environment variables?

You can use environment variables in Plugin, Power Automate, Model driven apps and canvas apps as well. Below is a sample code to fetch a environment variable – new_MainContent. As stated earlier environment variable default value and current value are stored in different entities.

 //query default value and current value of a environment variable
            var fetch = string.Format(@"<fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'>
                        <entity name='environmentvariabledefinition'>
                            <attribute name='environmentvariabledefinitionid'/>
                            <attribute name='schemaname'/>
                            <attribute name='valueschema'/>
                            <attribute name='type'/>
                            <attribute name='displayname'/>
                            <attribute name='description'/>
                            <attribute name='defaultvalue'/>
                          <filter type='and'>
                              <condition attribute='schemaname' value='new_MainContent' operator='eq'/>
                            </filter>
                        <link-entity name='environmentvariablevalue' alias='ab' link-type='inner' to='environmentvariabledefinitionid' from='environmentvariabledefinitionid'>
                          <attribute name='value'/>
                        <filter type='and'>
                                <condition attribute='value' operator='not-null'/>
                              </filter>
                            </link-entity>
                          </entity>
                        </fetch>");
            var result = client.RetrieveMultiple(new FetchExpression(fetch));
            var defaultVal = result.Entities.ElementAt(0).GetAttributeValue<string>("defaultvalue"); // default value
            var currentVal = result.Entities.ElementAt(0).GetAttributeValue<AliasedValue>("ab.value").Value;
// current value. Check for null condition before this statement

You will also like the below posts

Hope this helps!
Debajit Dutta
(Business Solutions MVP)