Recently, while setting up sitecore, using the official documentation, for syncing the content from XM Cloud to local, it required multiple commands. First need to login, then to connect to environment based on ID and then connecting local environment and then pulling and pushing.
Although when I did this first time, it helped me understand the cloud plugin and gained knowledge about the actual steps involved in the process, but on a large scale, when you wish to automate the entire local environment setup for your project or when there is a new developer who’s onboarded to the project or when you have to do the same process for the nth time, wouldn’t it be better if we can do this via a single script?
Also we cannot remember the environment id easily when compared with the environment name…So thought of writing a powershell script for this syncing process based on the project name and environment name.
As in XM Cloud, a single subscription can have multiple projects and each projects can have multiple environments, it possible to have environment with same name among different projects, thus the need for project name and environment name.
#SyncLocal.ps1 - Pulls the content from the specified project's environment to local
param ($projectName, $envName)
Write-Host "Logging in to XM Cloud"
dotnet sitecore cloud login
Write-Host "Getting Project Lists"
$var = dotnet sitecore cloud project list --json | ConvertFrom-Json
Write-Host "Total" $var.Count "Projects Found!"
foreach($project in $var)
{
if($project.name -eq $projectName)
{
Write-Host "Identified " $projectName
Write-Host "Checking Environment " $envName "in " $project.name
foreach($environment in $project.environments)
{
$env = dotnet sitecore cloud environment info -id $environment --json | ConvertFrom-Json
if($env.name -eq $envName)
{
Write-Host "Identified " $env.name
Write-Host "Attempting Connection"
dotnet sitecore cloud environment connect -id $environment --allow-write true
Write-Host "Connected"
}
}
}
}
Write-Host "Connecting to Local Environment"
dotnet sitecore connect --ref xmcloud --cm https://xmcloudcm.localhost --allow-write true -n local
Write-Host "Pulling contents from " $projectName "-" $envName
dotnet sitecore ser pull -n $envName
Write-Host "Pushing contents to local"
dotnet sitecore ser push -n "local"

Of course, this script should be fine-tuned if you need some error handling. The main idea behind this post is to highlight the ways we can extend Sitecore cloud plugin via PowerShell for our custom requirement.
Make sure you update your local environment name as per your setup.



























































































