We have lots of media items and our business wants to copy the data from en version of media item to all other language versions defined in System/Languages. This ensures that media is available in all the languages. So, we created the below powershell script to achieve the same -
#Get all language versions defined in System/Languages
$languages = Get-ChildItem /sitecore/System/Languages -recurse | Select $_.name | Where-Object {$_.name -ne "en"} | Select Name
#Ensuring correct items are updated by comparing the template ID
$items = Get-ChildItem -Path "/sitecore/media library/MyProjects" -Recurse | Where-Object {'<media item template id>' -contains $_.TemplateID}
#Bulk update context to improve performance
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
foreach($item in $items){
foreach($language in $languages){
$languageVersion = Get-Item -Path $item.Paths.Path -Language $language.Name
#Check if language version already exists
if($languageVersion -eq $null){
Write-Host "Creating "$language.Name" version for "$item.Name
Add-ItemLanguage -Item $item -Language "en" -TargetLanguage $language.Name -IfExist OverwriteLatest
}
}
}
}
Comments
Post a Comment