9

Is there a way in Sitecore Powershell Extensions to create new language versions of a branch in the content tree? I need it to copy exactly the presentation details set on the current language to the new language.

I've found this article https://www.cognifide.com/our-blogs/sitecore/quickly-create-new-language-versions-on-your-sitecore-cms/ so would like to do the same but with SPE.

3 Answers 3

14

There is already one blog post on this topic on Internet

Code snippet from the blog as below:

Add-ItemLanguage -Path "master:\sitecore\content" -Language "en" -TargetLanguage "de-DE" -IfExist OverwriteLatest  -IgnoredFields ""

For recursive you can try below PowerShell script

Get-ChildItem /sitecore/content/home -Recurse |
  Where-Object { $_."TemplateName" = "Sample Item" } |
  ForEach-Object { Add-ItemLanguage $_ -Language "en" -TargetLanguage "de-DE" -IfExist OverwriteLatest  -IgnoredFields "" }
7

Another attempt with creation of new language versions of both items in the branch as well as items referenced through datasources:

#Delete the following line to perform the operation
# otherwise it only shows what it will do and not perform any changes
$WhatIfPreference = $true

function Add-TranslationForItemAndDataSources {
    [CmdletBinding()]
    param(
        [Item]$Item,
        [string]$Language,
        [string]$TargetLanguage
        )

    # change the -IfExists From "Skip" to "Append" 
    # if you want to create a new version even if it exists
    Add-ItemLanguage -Item $item -Language $Language -TargetLanguage $TargetLanguage -IfExist Skip

    Get-Rendering -Item $item -FinalLayout -Device (Get-LayoutDevice -Default) | 
        Where-Object { -not [string]::IsNullOrEmpty($_.Datasource)} | 
        ForEach-Object { Get-Item "$($item.Database):" -ID $_.Datasource }
        Add-ItemLanguage -Item $item -Language $Language -TargetLanguage $TargetLanguage -IfExist Skip
}

Get-ChildItem master:\content -Recurse |
  Where-Object { $_.TemplateName -eq "Sample Item" } |
  ForEach-Object { Add-TranslationForItemAndDataSources -Item $_ -Language "en" -TargetLanguage "de-DE" }
2
0

This is in addition to the answers from Adam and Harsh. If your branch has large number of child items and you are adding multiple language versions to each item, adding language version will be time taking process.

To address this, you should set BulkUpdateContext in your scripts to tell Sitecore explicitly not to trigger events and pipelines while the language versions are being created. In such case the script will look like -

$languages = Get-ChildItem /sitecore/System/Languages -recurse | Select $_.name
    New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {           
        $query= "fast:<Path to branch of content tree whose children need language version>/*"
        $items = Get-Item -Path "master:" -Query $query
        foreach($item in $items){
           foreach($language in $languages){
                $languageVersion = Get-Item -Path $icon.Paths.Path -Language $language.Name
                #check if language version already exists
                if($languageVersion -eq $null){                                     
                    Add-ItemLanguage -Item $icon -Language "en" -TargetLanguage $language.Name -IfExist OverwriteLatest                     
                }   
            } 
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.