Friday, May 10, 2019

Sitecore Rule Based Configuration

Sitecore Role Configuration

Starting from Sitecore 9, you can use Rule Based Configuration to choose which configuration should be applied for each Sitecore instance. Sitecore already has 2 predefined rules, Role and Search, but we will also use a custom one.
You can find the documentation here: Rule-based configuration

What is needed

We had 3 server roles (Standalone, Content Delivery, Content Management) for our instances. The instances are configured according to its roles and that was causing too many config files. 

What is achieved

  • Made configuration settings dependent on a rule
  • Easy to manipulate, assign

How

If you hadn't already separate your AppSettings, you can start by doing that. Then you should simply add a reference to your AppSettings on web.config. 

<appSettings configSource="App_Config\AppSettings.config" />

On your AppSettings.config file, you can define your rules.

But also, you can separate each environment and define these environment specific settings there. You should add a reference to these files and name them same as your referenced file at top of your main AppSettings.config:

<appSettings file="EnvironmentAppSettings.config">

You can add the following to EnvironmentAppSettings.config for your local environment for rule configuration: 

<add key="role:define" value="Standalone" />
<add key="env:define" value="Local"/>

EnvironmentAppSettings should include what you need at this point, for instance Acceptence Content Delivery server can have the following: 

<appSettings>
<add key="role:define" value="ContentDelivery"/>
<add key="env:define" value="Acceptance"/>
</appSettings>

To start using these rules, whether they are custom or not, in your configuration, you should also register them at top. For example, following adds the custom "env" rule:

<configuration xmlns:env="http://www.sitecore.net/xmlconfig/env">

Then you can use them in each setting:

<sc.variable name="dataFolder" env:require="Acceptence" value="\Data" />

You can customize rule values according to your needs. If you need a setting to work on your both Acceptence and Production environments: 

env:require="Acceptence or Production"

or you want it to work on only Content Delivery server: 

env:require="Acceptence or Production" role:require="ContentDelivery"

or you want it not to work on one environment: 

env:require="!Local"