Step-by-Step Guide to Implementing Environment Variables in Dynamics 365
You can use environment variables in Dynamics 365 by putting them in your solutions and using them for setup. Environment variables help you handle important settings, like URLs or passwords, without changing the code. They make your job easier and safer. When you use environment variables, you keep your settings neat and ready for any place. Learning this skill gives you more control and helps you feel sure about your projects.
Key Takeaways
Use environment variables to store important settings like URLs and passwords outside your code. This makes your solutions safer. It also makes them easier to update.
Make environment variables in your solution. Give them clear names. Pick the right data types. This helps you manage settings for development, testing, and production easily.
Use environment variables in Power Automate flows, plugins, and JavaScript. This stops you from hardcoding values. It helps your apps work in different environments.
When you move solutions between environments, always change the environment variable values. Test your setup to stop errors. This keeps your settings the same.
Keep secret data safe by using the 'Secret' type for environment variables. Control who can see or change these sensitive values.
Environment Variables Overview
Purpose and Benefits
You use environment variables in Dynamics 365 to handle important settings for your apps and flows. These settings can be things like API URLs, organization IDs, or secret keys. When you put this information in environment variables, you do not need to put values inside your code. This makes your solutions easier to fix and safer to use.
Tip: Environment variables let you keep all your setup data in one spot. You can change a value for development or production without changing your app code.
Here are some ways you use environment variables:
Save the environment type so your app knows if it is in development, testing, or production.
Handle domain names and API URLs that are different in each environment.
Keep private keys or credentials safe and not in your app code.
Use feature flags to turn features on or off for each environment.
When you use environment variables, your solutions become more flexible. You also make fewer mistakes because you do not have to change your code for every environment. This helps your team work together, since you can share solutions without showing secret data. It also helps you follow your company’s security rules by keeping secrets out of your code and under strong controls.
Key Components
Each environment variable in Dynamics 365 has two main parts:
Default Value: This value is the same for all environments. It is used if no other value is set.
Current Value: This value is for each environment. You can set a different value for development, testing, or production.
For example, you might use a default API URL for development. When you move your solution to production, you set a new current value for the production API URL. Your app then uses the right value for each environment without changing any code.
Create Environment Variables
Add to Solution
You can put environment variables in your solution by using the Power Apps Maker portal. This helps you keep all your settings for apps and flows together. Here are the steps to add environment variables:
Pick your environment or make a new one from the "Environments" tab.
Open the solution where you want to add environment variables.
In the solution, look for the "Environment Variables" section.
Click "New variable" to make a new environment variable.
Type a name for your variable. Choose a name that shows what it controls, like "API_Base_URL" or "Feature_Toggle".
Pick the data type for your variable. You can pick from Text, Decimal Number, JSON, Data Source, Yes/No, or Secret.
Add a default value for the variable. This value is used if you do not set a special value for an environment.
Save the variable by clicking "Create".
Tip: Give your environment variables names that are easy to understand. This helps you find and manage them later.
When you add environment variables, you make one place for all your setup data. This lets you change settings without changing your app or flow. You also make things safer by choosing who can see or change these variables.
Set Types and Values
It is important to pick the right type and value for each environment variable. The type you choose changes how your app or flow uses the variable. Here are the main types you can use:
When you set values, always add a default value. This value is a backup if you do not set a special value for an environment. For example, you might use a test API URL as the default and use a different value for production.
Best Practice: Make the variable name match what it controls. For example, use "SharePoint_Site_URL" for a SharePoint site link. This makes it easy to find and update variables.
You can change the value of an environment variable any time. Go to the solution, find the variable, and change its current value. This lets you change settings for development, testing, or production without changing your app or flow.
Common Pitfalls to Avoid
Hardcoding values: Do not put API keys, URLs, or feature toggles right in your app or flow. This makes updates hard and can cause mistakes.
Wrong data type: Always pick the right type for your variable. Using the wrong type can cause errors or things not to work right.
Missing default values: If you forget to add a default value, your app or flow might not work in some environments.
Not updating values after deployment: Remember to set the right value for each environment after you move your solution. This makes sure your app uses the right settings.
Note: Keep secret data, like passwords or API keys, as secret environment variables. This keeps them safe and locked.
By following these steps and tips, you make your solutions easier to use and safer. Environment variables help you handle settings in all your environments with less work.
Use in Workflows and Code
You can use environment variables in many places in Dynamics 365. They help you set up flows, plugins, and scripts without putting values in the code. This part will show you how to use them in Power Automate flows and in custom code like plugins and JavaScript.
Power Automate Flows
Power Automate flows help you do tasks automatically in Dynamics 365. When you make flows in a solution, you can use environment variables to make your flows safer and easier to change.
How to Reference Environment Variables in Flows:
Open your solution in Power Apps.
Make or change a cloud flow.
In your flow, find the step where you need a setting, like a URL or key.
For simple fields, type the value or pick it from the dynamic content list.
For lookup fields, pick "Enter custom value" at the bottom of the list.
In the dynamic content pane, pick the environment variable you want.
Tip: If you add a new environment variable, close and open the flow designer again. This updates the list so you can see your new variable.
When you use environment variables in your flow, you can change settings for each environment without changing the flow. For example, you can use a different SharePoint site URL for testing and production. If you change the value in the environment, save or turn your flow off and on to use the new value.
Store API keys or connection strings safely.
Set URLs for SharePoint, APIs, or other services.
Use feature toggles to turn parts of your flow on or off.
Manage branding details like logos or colors.
Note: You can only use environment variables in flows that are inside a solution. Flows outside solutions cannot use them.
Plugins and JavaScript
Sometimes you need to use environment variables in custom code, like plugins or JavaScript. These settings help your code work in any environment without changes.
How to Access Environment Variables in Plugins:
Plugins run on the server and can read environment variables from special Dataverse tables. The tables are called environmentvariabledefinition
and environmentvariablevalue
. Your plugin code can look in these tables to get the current value for each variable. If there is no current value, the code uses the default.
Here is a simple way to get environment variables in a plugin:
// Example: Get environment variable value in a plugin
public string GetEnvironmentVariableValue(IOrganizationService service, string schemaName)
{
// Look in environmentvariabledefinition table for the variable
// Look in environmentvariablevalue table for the current value
// Return current value if found, else return default value
}
Tip: Save the values during plugin execution. This makes your plugin faster and uses the database less.
How to Use Environment Variables in JavaScript:
JavaScript runs on the client side, so it cannot read environment variables directly. You can fix this by making a custom Web API. This API reads the variable from Dataverse and sends it to your script. Your JavaScript code calls the API and uses the value it gets.
Steps:
Make a custom Web API that looks in the environmentvariabledefinition and environmentvariablevalue tables.
Call this API from your JavaScript code.
Use the value you get in your script.
Note: This way works for both Model-driven Apps and Canvas Apps.
Tips for Special Scenarios:
If you cannot get the variable right away, use a helper API or a plugin to get the value.
Always check if the value is set for the current environment. If not, use the default.
Use JSON type variables for settings that need more than one value.
Common Use Cases:
Store API endpoints or credentials for plugins.
Set feature flags to control logic in your code.
Manage URLs or IDs that change between environments.
Using environment variables in your code helps you avoid putting values in the code. You can move your solution between environments with less work and fewer mistakes.
Deploy and Manage Across Environments
Export and Import Solutions
You have to move your solutions to different places, like development, testing, and production. This helps your apps and flows work the same everywhere. First, sign in to Power Apps and go to Solutions. Pick the solution you want to move. Click export and follow the steps to make a zip file. This file has all your parts, including environment settings.
To import a solution, go to the new environment and click import. Find your solution file and upload it. The system will show you steps, like checking details and setting up connections. If your solution uses environment settings, you will need to enter values for each place. Make sure you put the right values for the new environment. If you forget any needed values, the system will warn you so you can fix them before you finish.
Tip: Always check and test your solution in a staging place before moving it to production. This helps you find problems early.
Update Values Post-Deployment
After you move your solution, you might need to change some values for each environment. You can do this with deployment settings files. These files keep the values for each place in a simple way, like JSON. Change the file with the right values for your target place, like URLs or keys. Then, add this file to your build pipeline. When you run the release pipeline, connect the settings file so the system updates the values for you.
Doing this saves time and stops mistakes. You do not have to update things by hand, and your settings stay the same everywhere. If you need to change a value later, just update the settings file and move it again. This way also helps you undo changes if something goes wrong. You can keep track of changes in source control and use versioning to manage updates.
Try to automate as much of your deployment as you can. This makes mistakes less likely and keeps your updates safe.
When you use environment variables for settings, you get many good things.
Your team can find all settings in one spot.
You follow rules and make checking easier.
If you want to do more, try these ideas:
Use DevOps tools to automate deployments.
Always use easy-to-understand names and change values when your environments change.
FAQ
How do you update an environment variable after deployment?
First, open your solution in Power Apps. Next, look for the environment variable you want to change. Change the current value so it fits your new environment. Make sure to save what you changed. Now, your app or flow will use this new value.
Can you use environment variables outside of solutions?
No, you cannot use environment variables in flows or apps that are not inside a solution. You must put your flows and apps into a solution before you can use environment variables.
What happens if you forget to set a default value?
If you do not add a default value, your app or flow might not work in some places. Always add a default value so you do not get errors.
Are environment variables secure for storing secrets?
You can keep secrets safe by using the "Secret" type. This keeps private data protected. Only people with the right permissions can see or change secret values.
Can you use environment variables in both Power Automate and custom code?
Yes! You can use environment variables in Power Automate flows, plugins, and JavaScript. Each one has its own way to get the values.
Tip: Always test your setup in every environment to make sure it works right.