Environment variables in Sitecore configs

Environment variables in Sitecore configs

I just recently found out that Sitecore have been supporting using Windows environment variables in config files since Sitecore 8.2 rev. 160729 (Initial).

I was digging around in Sitecore, looking at the ConfigReader for another personal project I was working on, when I noticed these lines:

// ...

reader.ExpandIncludeFiles(ConfigReader.config.DocumentElement, new Hashtable());
reader.LoadAutoIncludeFiles(ConfigReader.config.DocumentElement);
reader.ReplaceGlobalVariables(ConfigReader.config.DocumentElement);
reader.ReplaceEnvironmentVariables(ConfigReader.config.DocumentElement);

// ...

Especially the last line - ReplaceEnvironmentVariables.

As mentioned, this feature was introduced in the intial release of Sitecore 8.2, so actually quite a while ago.

To use environment variables in your config files just write it like $(env:YOUR_CUSTOM_VARIABLE). This is pretty much like how global variables are used, e.g. $(dataFolder).

For example $(env:COMPUTERNAME) would be replaced with the local computer name.

This is quite a neat feature that can be very useful, for example in scenarios like multi-server setups where you might want to use machine-specific settings and the like.