Sitecore use global and environment variables in config files

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:

Global Environment Variables

Global Environment Variables

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:

 

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, General .NET, Sitecore 8, Sitecore 9 and tagged , , , . Bookmark the permalink.

1 Response to Sitecore use global and environment variables in config files

  1. Pingback: Working with environment variables in your Sitecore solution – Let AZURE CLI guide you – Visions In Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.