How to use SXA site definition Environment field

SXA is an excellent framework used to speed up the production of websites and reuse the components, layouts, and templates across different sites.

While working on SXA with either single site or multi-site architecture, we often require to set up different domain values for each environment like development, staging, and production.

When we create any site in the SXA, it stores all the site information like hostname, target hostname, startItem, database, etc., which are part of the site definition in the database instead of a Sitecore configuration file. You can review all the related fields by expanding the content tree up to the site definition item, which is available inside the Settings node of each site. Refer to the below screenshot for where to look in the content tree.

In this article, we will learn about the field named Valid for environment. You can see that field in the above image with a value *. We also have the value * for the Host Name field, which means any SXA will handle any hostname request and resolve this site.

Now let us provide some specific value for the fields and try to understand how that works. While observing the below image, each field has some specific value applied.

Here, the field Valid for environment holds the value local. Now the question arises of how SXA will know against which value to compare the value of this field and apply that specific site setting?

So, the quick answer is, SXA has a setting variable named ‘XA.Foundation.Multisite.Environment‘. You can set this value through the Sitecore config patch file, and SXA will use this setting value to load the matching site information.

So, let’s say I’ve another site setting named coveo-staging, as shown in the image below.

Concerning the above image, my setting variable should look like the following. Based on this variable value, SXA applies the specific site information.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:security="http://www.sitecore.net/xmlconfig/security/">
	<sitecore>
		<settings>
			<setting name="XA.Foundation.Multisite.Environment" value="staging" />
		</settings>
	</sitecore>
</configuration>

However, again there is one glitch. Do I have to maintain a separate patch file for each environment? Can it be possible in a single solution? Is there any way to have only one patch file, and the variable’s value should be loaded from somewhere like the environment variable?

So, the answer is Sitecore patching can read and apply the environment variable values while loading the patch configurations. In this case, the above setting variable in the patch file should look like the following.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:security="http://www.sitecore.net/xmlconfig/security/">
	<sitecore>
		<settings>
			<setting name="XA.Foundation.Multisite.Environment" value="$(env:SITECORE_ENVIRONMENT)" />
		</settings>
	</sitecore>
</configuration>

This environment variable approach is also applicable when you run the Sitecore + SXA in the Docker and Kubernetes container.

Happy Sitecore learning…!!!

Jatin Prajapati's Blog

Some little contribution to Sitecore community