Streamlining Multi-Language Content Management in Sitecore

Managing multi-language websites with Sitecore can be a challenge, especially when we need to replicate many field values across different language versions of items. We need to manually copy field values from one language to other language version, which is very time consuming.

We can achieve this with PowerShell script in three easy steps. 

- Fetch an item using item path or item ID

- Iterate through its fields

- Copy values from source language to target language

Below is the overview of the script and the complete script to do that. 

Script Overview:

  1. Set Item Path & Languages:
    • $itemPath specifies the path to the Sitecore item.
    • $sourceLanguage and $targetLanguage are the language codes for the source and target languages (in this case, English and French).
  2. Fetch Item:
    • $item = Get-Item -Path $itemPath retrieves the item from Sitecore using the specified path.
  3. Editing Block:
    • $item.Editing.BeginEdit() begins editing the item.
  4. Field Value Copy:
    • It loops through the fields of the item and checks if the field has a value in the source language ($field.HasValue($sourceLanguage)).
    • If it does, the value from the source language is copied to the target language using $field.SetValue($value, $targetLanguage).
  5. Save Changes:
    • After modifying the fields, $item.Editing.EndEdit() saves the changes.
  6. Error Handling:
    • If an error occurs, the editing process is cancelled ($item.Editing.CancelEdit()), and an error message is logged.
Complete Script:

$itemPath = "/sitecore/content/Home/MyItem"
$sourceLanguage = "en"  # English
$targetLanguage = "fr"  # French

# Get the item from Sitecore
$item = Get-Item -Path $itemPath

try {
    $item.Editing.BeginEdit()

    foreach ($field in $item.Fields) {
        # Check if the field has a value for the source language
        if ($field.HasValue($sourceLanguage)) {
            $value = $field.GetValue($sourceLanguage)

            # Optionally, print verbose output for debugging
            Write-Host "Copying field: $($field.Name) from $sourceLanguage to $targetLanguage"
            
            # Set the value for the target language field
            $field.SetValue($value, $targetLanguage)
        }
    }

    # Save the item after updating the field values
    $item.Editing.EndEdit()

    Write-Host "Fields successfully copied from $sourceLanguage to $targetLanguage"
}
catch {
    # In case of an error, cancel the edit and log the error
    $item.Editing.CancelEdit()
    Write-Error "Error migrating fields: $_"
}

I hope this will help and reduce some load for copying field value one by one!

Stay connected for more updates. We will see some more useful PowerShell scripts in the next blogs.

Thank you!

Comments

Popular posts from this blog

Setting Up Sitecore Headless with Next.js in Disconnected Mode

Translating Sitecore items’ field values into different language with PowerShell and Translate API

Create Sitecore Ribbon Button using Sitecore PowerShell Extensions