2

I have a script that copies a page with renderings from a set language to 1 or more other languages that are selected by checkboxes. The whole script works when i run it in the ISE, but when I run it from the Context Menu it doesn't seem to find any renderings and therefore it only copies the page (with renderings), however the data source items are not copied. Thankful for any help I can get!

$sourceItem = get-item .
$sourceLanguage = $sourceItem.Language.Name

$options = @{
    "Danish"="da-DK"
    "Norwiegan"="nb-NO"
    "German"="de-DE"
}

$result = Read-Variable -Parameters `
 @{ Name = "languageItem"; Title="Choose The Target Language"; 
     Options=$options;
 editor="check"} `

 -Title "Create Language Version Script" -Width 400 -Height 200 -
OkButtonName "Create Version" -CancelButtonName "Cancel" -ShowHints

if($result -ne "ok")
{
    Exit
}

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


# adds new version of the page
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 { 
        Write-Host $_.Datasource
        $datasourceItem = Get-Item "$($item.Database):" -ID $_.Datasource
        Write-Host "$($item.Database):"
        Write-Host $datasourceItem.Name
        Add-ItemLanguage -Item $datasourceItem -Language $Language -
        TargetLanguage $TargetLanguage -IfExist Append        
    } 
}


foreach ($i in $languageItem)
{ Add-TranslationForItemAndDataSources -Item $sourceItem -Language "sv-SE" -
TargetLanguage $i }

write-host 'Script ended'
  • Make sure that you don't have anything in your editing session. Switch your Session from ISE editing session to Dispose session ... and see if it still works in ISE, maybe you've got some temp value which is missing while executing script in different context. Add more log messages and simply debug it step by step. Use Write-Host to display states in different places and compare it with those from ISE. – Alan Płócieniak May 9 '17 at 12:36
  • Thank you for your answer! Unfortunately it still worked with Dispose session. I also added a number of Write-Host and tried a number of other functions for example Get-Layout, which strangely work in both context. So it seems to be exclusively Get-Renderings. Any other ideas? – Nils Hall May 9 '17 at 13:39
1

When you're in PowerShell ISE, you can select the context language. From the context menu however, you don't have that luxury.

I have yet to figure out how to detect which language is currently selected in Content Editor, and in any case it's probably best to ask the user which is the source language from the list of available languages on the source item. For that, you could replace the first line of your script with this:

$contextItem = gi .
$sourceOptions = @{}
$contextItem.Languages | 
    Where {(gi $contextItem.FullPath -Language $_.Name).Versions.Count -ge 1} |
    % {$sourceOptions.Add($_.Title, $_.Name)}
if ($sourceOptions.Count -gt 1) {
    $sourceResult = Read-Variable -Parameters @{ 
        Name = "sourceLanguage";
        Title = "Choose The Source Language"; 
        Options = $sourceOptions;
        editor = "dropdown"} `
        -Title "Create Language Version Script" -Width 400 -Height 200 `
        -OkButtonName "Continue" -CancelButtonName "Cancel" -ShowHints

    if($sourceResult -ne "ok") { Exit }
} else {
    $sourceLanguage = $sourceOptions.Values[0]
}

$sourceItem = gi -Path $contextItem.FullPath -Language $sourceLanguage

# The rest of your script goes here
$sourceLanguage = $sourceItem.Language.Name
# ...

This will present you with a dropdown list of available source languages for the item. If the item exists in only one language, it will auto-select that language and skip the dialog.

The key here is this line:

$sourceItem = gi -Path $contextItem.FullPath -Language $sourceLanguage

where the source-language-specific variant of the item is selected. With this in place, Get-Rendering will correctly select the final layout for that language.

If you opt for this path, you also will probably want to remove the hard-coding of the source language (sv-SE) found in your sample code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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