Transferring Content Across Environments in Sitecore XM Cloud
Recently, I encountered a scenario where it was necessary to transfer content from one environment to another using Sitecore Content Serialization. In this blog post, I will provide a step-by-step guide that outlines the essential steps for successful content synchronization.
Let’s delve into the detailed instructions to ensure a successful content transfer.
Prerequisites —
Before beginning this process, ensure that you have installed the .Net 8 SDK, which can be downloaded from https://dotnet.microsoft.com/en-us/download/dotnet/8.0
Step 1 — Create a folder in your machine you can name as per your wish in my case I have created with the name SitecoreRepo under C drive
Here is the path of the folder C:\SitecoreRepo
Step 2 — To use a tool for local access for current directory and subdirectories it needs to be added to manifest file for that here inside our created folder we need to run the below command
C:\SitecoreRepo>dotnet new tool-manifest
Step 3 — Run below command to add package source of Sitecore
PS C:\SitecoreRepo> dotnet nuget add source -n Sitecore https://nuget.sitecore.com/resources/v3/index.json
Step 4 —Next, proceed by installing the Sitecore CLI
dotnet tool install Sitecore.CLI
Step 5 — Once Sitecore.CLI installed successfully let’s login to Sitecore Cloud Instance by following command
dotnet sitecore cloud login
Once you execute the command mentioned above, you might encounter an error. To resolve this, please run the following command, which will fix the issue and allow you to successfully log in to the Sitecore Cloud portal.
dotnet sitecore init
dotnet sitecore plugin add -n Sitecore.DevEx.Extensibility.XMCloud
once above commands run successfully, please run
dotnet sitecore cloud login
As soon as you run the command mentioned above, a new browser window will open as depicted below. Here, you will need to confirm and authorize the action.
after confirmation you will see below screen
Step 6 — Now login to https://portal.sitecorecloud.io/ go to the option XM Cloud Deploy -> Select the option Projects -> after that take the ID in front of the desired project to connect.
once you have project ID copied, please run below command to get the environment list of project ID copied.
dotnet Sitecore cloud environment list --project-id XXXXXXXXXX
after running the command, you will get below screen where all details are listed now you can copy the environment ID from below from where you want to copy the content. (Source Environment)
Step 7— Run below command on copied environment ID
dotnet sitecore cloud environment connect --environment-id XXXXXXXXXX --allow-write
previous command will create a new section into the file structure
C:\SitecoreRepo\.sitecore\user.json like this
"xxx-Dev": {
"ref": "XmCloud",
"allowWrite": true,
"host": "https://xxxxxxxxxxxx.sitecorecloud.io/",
"variables": {}
}
Now you must create a new folder into C:\SitecoreRepo\.sitecore\ called “content”
inside content folder please create a new json file called content.json
now add the following line to content.json file these are item paths which we need to move
{
"$schema": "../../.sitecore/schemas/ModuleFile.schema.json",
"namespace": "content",
"items": {
"includes": [
{
"name": "Home",
"path": "/sitecore/content/Home",
"scope": "ItemAndDescendants"
}
]
}
}
Now you must update the file sitecore.json to add the new file, it’s located on the root of your project folder.
after saving above file please run below below command to copy content form source environment.
you will see how many files got copied message on command window as well as a new folder called serialization will be created with the respective yaml files
please follow the screenshots
dotnet sitecore serialization pull -i Content -n environmentName
Step 8 — Now final step where we need to move this content to target environment
before running the next command let’s connect to the target by running the command mentioned in step 7
dotnet sitecore cloud environment connect — environment-id XXXXXXXXXX — allow-write
Step 9— now run the following final command which will do the whole magic to push content to different environment
dotnet sitecore serialization push -i Content -n environmentName
This command will send all the data to the target environment, you must replace the environmentName for the required environment to send the data
check your target environment and see the files which we moved.
By following the steps outlined, we successfully moved content between environments.
Moving content with packages can be time-consuming. By using the method described above with the Sitecore CLI, it takes significantly less time and works like magic.
Happy XM Cloud Learning …