Making your site configurations more readable

This is just a quick post on an approach I landed on for a recent project that I thought would be worth sharing with the community.

Prior to Sitecore Configuration Roles ( https://github.com/Sitecore/Sitecore-Configuration-Roles), if you wanted to have multiple hostnames associated with a given site, you had to do something like this:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <sites>
      <site name="site1"
	    inherits="website"
	    patch:before="site[@name='website']"
            hostName="site1.mycompany.local|site1cm-dev.mycompany.com|site1cm-qa.mycompany.com|site1.mycompany.com" />
    </sites>
  </sitecore>
</configuration>

And while this isn’t a bad approach, I find it a bit hard to read with all of the subdomains in one line.

With the introduction of Sitecore Configuration Role, which is built-in to Sitecore 9.0 and above, here’s how I’ve been configuring my multiple subdomains for my sites per environment:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role" xmlns:instanceName="http://www.sitecore.net/xmlconfig/instanceName">
  <sitecore>
    <sites>
      <site name="site1"
	    inherits="website"
	    patch:before="site[@name='website']"
            hostName="site1.mycompany.local" />

      <site name="site1" instanceName:require="DEVCM"
            set:hostName="site1cm-dev.mycompany.com" />

      <site name="site1" instanceName:require="DEVCD"
            set:hostName="site1-dev.mycompany.com" />

      <site name="site1" instanceName:require="QACM"
            set:hostName="site1cm-qa.mycompany.com" />

      <site name="site1" instanceName:require="QACD"
            set:hostName="site1-qa.mycompany.com" />

      <site name="site1" instanceName:require="PRODCM"
            set:hostName="site1cm.mycompany.com" />

      <site name="site1" instanceName:require="PRODCD"
            set:hostName="site1.mycompany.com" />
    </sites>
  </sitecore>
</configuration>

Beyond the readability improvement, this also helps you ensure that you’ve covered all of your environments since you’ve got an individual line for each environment. Another benefit I found from this approach is that, when you launch the /sitecore/admin/showconfig.aspx page, Sitecore will show you the hostName that matches the current environment only.

This approach does require adding one line to your root web.config in the appSettings section

<appSettings>
    ...
    <add key="instanceName:define" value="PRODCD" />
    ...
</appSettings>

And that’s it! I hope this helps you and gives you some ideas on better ways of organizing your Sitecore configuration.

Drop a comment and let me know if you’ve done something similar or if you’ve got another approach.

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