Disable Item Cloning in Sitecore using patch config with .env file variable toggle

I recently learnt from someone that when you migrate content from one environment to another, if cloned items are existent, content migration will result in errors and so, it is good to disable ItemCloning setting in target environment just while performing the migration. Although I vaguely remember facing a similar situation in the past, this scenario is more relevant now since migrating content to XM Cloud is going to be very common across projects. Since Sitecore has a ItemCloning.Enabled configuration setting and it is as simple as patching this setting as per your need. As with any scenario, all that matters in a technical world is, how you change this setting dynamically instead of altering/copying-over the patch file manually. So, this blog post is about a few different ways to accomplish the patching dynamically. While the first one is the most common approach, I felt there is no harm in adding a couple of other unconventional approaches that stroked me. Once i completed, all of these seemed roundabout to me since you have to start/up the instance which means manual intervention. So, on any day, i will alter the config setting manually before running the up script for the clone setting to take effect. Furthermore, add to the situation that you won't disable item cloning on a daily basis and such content migration is a planned activity. Then, you never know what will be useful when in life so, I decided to go full length, do everything needed and bring all options on table for better decision-making. So, here are my thoughts/opinions/options:

1. Add a  variable to the .env file and make this env variable value available to the cm instance through docker-compose.yml and then patch the variable in the corresponding patch config. Additionally, use a switch in init.ps1 to set the .env variable during startup. Picture and screen shots for easier understanding:

.env file variable:


docker-compose.yml (line 89):

Patch config:


.\init.ps1 -initenv -EnableClone:


Sitecore configuration:


2. If you have a publish or deploy script, pass a switch that will determine if ItemCloning must be disabled. If yes, the publish script will drop the ItemCloning.Enabled setting patch pre-configured with a value of false. This is similar to how you manually add the setting in the patch config but just that the script will do it as part of copying files to the deploy folder after the build process.

Publish Script:


Then, a function like this in above script:


A patch like this:

****

****

So, the publish script switch decides the patch drop.

3. The third approach is unconventional - Add a placeholder for the boolean value in the patch config like <EnableClone>. This file will act as the template file for the patch config. Then, a PowerShell script will pick the specific template config and set-content for <EnableClone> placeholder to true or false based on the passed param to the PS script. This script can then be called as part of deploy process.

****

PS script to copy content from a pre-existing template file:

****


*****

The template file:

*****

Understandably, in case of the first approach all the following steps need to be navigated but the next two unconventional approaches "might" offer some kind of respite since only a patch is dropped and if you could copy that patch to the deploy location with PowerShell script, you "might" save valuable build/deployment time.

- down

- clean

- publish aka deploy (with necessary setting)

- up

Comments

Popular Posts