The Sitecore config files are a complex machine and it requires a healthy mind to work with it. Fortunately Sitecore have implemented a few tricks to ease our pain. This article focuses on 2 parts: global variable replacement and environment variable replacement.
GLOBAL VARIABLES:
Global variables are defined once and can then be reused in all your config settings.
Maybe you have seen these variables:
<sitecore> <sc.variable name="dataFolder" value="/App_Data" /> <sc.variable name="mediaFolder" value="/upload" /> <sc.variable name="tempFolder" value="/temp" /> </sitecore>
The sc.variable element defines the variable. The variables can then be used where you need them by writing $(variable_name):
<file value="$(dataFolder)/logs/myfile.log"/> <setting name="IndexFolder" value="$(dataFolder)/indexes" />
The above variables are just the built in ones. You can create your own variables and use them too.
ENVIRONMENT VARIABLES:
Since Sitecore 8.2 we have been blessed with environment variable replacements too.
Let’s say you have an environment variable on your machine:
You can add the environment variable to the config by writing $(env:variable_name):
<setting name="Environment" value="$(env:ASPNETCORE_ENVIRONMENT)" />
This is especially nifty as you can deploy the same config file to different environments and control your application per server.
MORE TO READ:
- Environment variables in Sitecore configs by Søren Kruse
- How to replace variables with values from connectionstrings or applicationsettings in sitecore config files by Bartłomiej Mucha