#Sitecore Rule Based Configuration for Newbies

If you are like me, you must play around with a new feature to understand it better. When Rule Based configuration was introduced in Sitecore 9.3 that was the case for me. Now that I understand it better and have used it in a real application, I will explain it so if I ever read this blog again, I will understand it.

cavemanrules

Role:Define

Depending on if you are running Sitecore locally or have setup a Content Management and Content Delivery server etc… this value will be different for each environment. In the Sitecore 9.3 you will find the following.

<!-- SUPPORTED SERVER ROLES 
Specify the roles that you want this server to perform. A server can perform one or more roles. Enter the roles in a comma separated list. The supported roles are:
ContentDelivery
ContentManagement
ContentDelivery, Indexing
ContentManagement, Indexing
Processing
Reporting
Standalone
Default value: Standalone
-->

In this case we will just use the default for our example.

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

Localenv:Define

This is used to denote the type of local environment installed. For instance, you can have a CM server setup for staging and production. You might then have one value on the CM for staging that says “StageCM” the one on the production CM could be “ProdCM”. This is done so you can break down the configuration settings further. I will go into this more later. In this case we will pretend the role:define value is Standalone and this is a developer’s install. We are going to add the following:

<add key="localenv:define" value="DevEnviroment"/>

Let’s bring this together. Below is a simple example of how to make sure the configuration looks for the role Standalone and localenv value of DevEnviroment. Notice the xmlns:localenv=http://www.sitecore.net/xmlconfig/localenv/. That is important to tell the process to look for that localenv key and xmlns:role=http://www.sitecore.net/xmlconfig/role/ is used to tell the process to look for the role key. In this example we are doing this for sites, but this can be done for other tags.

<?xml version="1.0"?>
<configuration
xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:localenv="http://www.sitecore.net/xmlconfig/localenv/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore role:require="Standalone">
<sites localenv:require="DevEnviroment">
//Add your transformation code here.
</sites>
</sitecore>
</configuration>

You can do many combinations. Keep in mind that you can use logical expressions as well. Like or and !.

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"xmlns:localenv="http://www.sitecore.net/xmlconfig/localenv/"xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore role:require="Standalone or ContentDelivery">
<sites localenv:require="LocalDeveloper">
//Add your transformation code here.
</sites>
<sites localenv:require="DevBuild"> //Add your transformation code here. </sites> <sites localenv:require="Prod"> //Add your transformation code here. </sites> <sites localenv:require="Stage"> //Add your transformation code here. </sites> </sitecore> <sitecore role:require="ContentManagement"> <sites localenv:require="Prod"> //Add your transformation code here. </sites> <sites localenv:require="Stage"> //Add your transformation code here. </sites> </sitecore> </configuration>

What is a good way to test all this? Well Sitecore has though of that.

Using [siteaddress]/sitecore/admin/showconfiglayers.aspx you can select a role and see what the configuration will look like. The result is similar to what you would see with showconfig.aspx.

One thing I should mention is so far, I have not gotten it to work with a non-Sitecore configuration transform. In those cases, you may still need to use something like SlowCheetah. Let me know if you have any questions.

Check out the links below for more reading on the rule based configurations.

https://doc.sitecore.com/developers/93/platform-administration-and-architecture/en/use-a-rule-based-configuration.html

https://jammykam.wordpress.com/2017/10/17/rules-based-configuration/