Sitecore 9 : Custom Config Roles

I recently wrote a detailed post about all the config changes in Sitecore 9. I highly recommend taking a read if you have not done so already.

I suggested in that post that we could introduce custom configuration roles in order to allow us to configure specific settings for different environments, such as UAT or Pre-Prod for example. After a conversation with Alen Pelin he told me of a much better way to achieve this, which is both cleaner and semantically more correct.

I assumed there was something hardcoded in code to worked against specific AppSettings keys during the patching process. Turns out there is way more magic happening here than that – we can define whatever keys we want and follow a convention to have those applied. Looking at it again and with this additional info, it turns out this is exactly the same as how the configuration works to switch search providers.

First off we can define a custom AppSettings key. The prefix of the key before the semi-colon can be whatever you want:

  <add key="env:define" value="UAT" />

Next in your custom configuration add a reference to the xml namespace. It needs to match your custom key and note the URL path also matches. Then following the same convention as config roles, you just add an env:require attribute to your setting nodes and magic…

<?xml version="1.0"?>
<configuration xmlns:env="">
      <setting name="Secret.Password" value="b" env:require="Local" />
      <setting name="Secret.Password" value="B" env:require="UAT" />
      <setting name="Secret.Password" value="β" env:require="Production" />

Want to go a bit crazy? You can use ALL THE THINGS! Yes, obviously don’t go crazy though…

<?xml version="1.0"?>
      <setting name="CMS.Provider" value="Sitecore" />
      <setting name="CMS.Provider" set:value="SiteCore" env:require="UAT" role:require="Standalone" />
      <setting name="CMS.Provider" env:require="Pre-Prod" role:require="Standalone or ContentManagement or ContentDelivery" >
        <patch:attribute name="value">ALL THE SITE CORES</patch:attribute>

So don’t do what I said before, do this instead. It feels much cleaner, and you’re not mixing an matching responsibilities.

Big thank you to Alen Pelin for reaching out and providing the additional info about the (more) correct way of doing this. He did an amazing job with the Configuration Roles and I’m really amazed at just how flexible it is.

Mrw GIFs - Find & Share on GIPHY


Leave a Reply

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

You are commenting using your 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