1

when I create new language version item of content using:

Add-ItemLanguage -Item $imageItem -Language $baseLang -TargetLanguage $targetLang -IfExist OverwriteLatest          

content page link field are empty on new version. In my current script I used linked items but I got error that I can't convert link to item:

function Create-LinkedItemsLangVersion{
        param(
            [string]$path
        )
      
        Write-Host "Started creating linked items for new language version..." 
      
        #Get all referenced items of selected source item
        $linkedItems = Get-Item -Path $path | Get-ItemReference -ItemLink
        
        #Loop through each linked item and create new
        forEach($linkedItem in $linkedItems)
        {
            #For each selected target languages
            foreach($targetLang in $newLang){
                
                #Add item to new language
                
                Write-Host 'Creating item ' $linkedItem.Name ' - ' $linkedItem.ID ' for a new language: ' $targetLang 
                
                Add-ItemLanguage -Item $linkedItem -Language $baseLang -TargetLanguage $targetLang -IfExist Skip
            }
        }
    }

   $generalLinkFields = $item.Fields | where {$_.Type -eq 'General Link'} 
        
         # Create Loop on all fields 
        ForEach ($generalLinkField in $generalLinkFields) 
        {  
             Write-Host 'Create new language version item for field: ' $generalLinkField.Name ' - ' $generalLinkField.Type ' - ' $generalLinkField.Title
    
            # Cast into GeneralLinkField
            [Sitecore.Data.Fields.LinkField] $field = $generalLinkField
            
             #Add this to get the Item
            $linkItem =  $field.Name 
            
            #Loop through new languages
            ForEach ($targetLang in $newLang ) {
                
                Write-Host "Creating new language version for media item: " $linkItem.Name ' - ' $linkItem.TargetItemID ' - ' $targetLang
                
                # Create Language Version of Image Item
                Add-ItemLanguage -Item $linkItem -Language $baseLang -TargetLanguage $targetLang -IfExist OverwriteLatest           
            }
        }
0

1 Answer 1

1

To resolve this error, you need to get the Target Item in the Loop to get the Item Object. So it should be like this.

function Create-LinkedItemsLangVersion{
        param(
            [string]$path
        )
      
        Write-Host "Started creating linked items for new language version..." 
      
        #Get all referenced items of selected source item
        $linkedItems = Get-Item -Path $path | Get-ItemReference -ItemLink
        
        #Loop through each linked item and create new
        forEach($linkedItem in $linkedItems)
        {

            #Add this to get the Item
            $targetItem = Get-Item -Path master:\ -ID $linkedItem.TargetItemID                

            #For each selected target languages
            foreach($targetLang in $newLang){
                
                #Add item to new language
                
                Write-Host 'Creating item ' $linkedItem.Name ' - ' $linkedItem.ID ' for a new language: ' $targetLang 
                
                Add-ItemLanguage -Item $targetItem -Language $baseLang -TargetLanguage $targetLang -IfExist Skip
            }
        }
    }
3
  • It doesn't work
    – user11621
    Apr 22, 2022 at 7:10
  • Can you check link field block
    – user11621
    Apr 22, 2022 at 7:11
  • You need to check for the properties of the link field to get either ID or Path and then get the item and use that. stackoverflow.com/questions/50978123/… Apr 22, 2022 at 7:38

Your Answer

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