1

I would like to know how to get all related media id's from page using Sitecore PowerShell module and create new language definition for media items.

So we have a page that have image field in data source, I would like to know how to gather all image field and media items, and for each selected to create new language version.

3 Answers 3

1

You can achieve this functionality using the below steps. I don't have ready script but I can guide you:

  1. You need to get all items in Recurse mode and need to apply ForEach loop like below code:
$allItems = Get-ChildItem -Path 'master://sitecore/content/home' -Recurse
$allItems | ForEach-Object {
    
}
    
  1. In this loop you need to get all image fields in the current item as well as in datasource using Get-ItemField command. More details are here

  2. After that you can create new version of media item using this command Add-ItemVersion. Link are here

1

You can use this approach and modify it as per your need.

$item =  Get-Item -Path "master:" -Uri "sitecore://master/{5003FB09-4ADF-410C-9F93-03EDF2FC137F}?lang=en-us&ver=1" # Get the Item here.

$imageFields = $item.Fields | where {$_.Type -eq 'Image'} # Get all the fields of type Image

# Create Loop on all fields 

ForEach ($imageField in $imageFields ) 
{  
     [Sitecore.Data.Fields.ImageField] $field = $imageField  # Cast into ImageField

     $imageItem = $newitem = Get-Item -Path master: -ID $field.mediaID   # Get Image Item from mediaID

     Add-ItemLanguage -Item $imageItem -Language "en-US" -TargetLanguage "en" -IfExist OverwriteLatest # Create Languaeg Version                    
}
0
0

You can follow these steps:

  1. Get all image fields included in a page.
  2. For each image field, get the image item.
  3. Create a new language version for each image item.

script :

# Specify the path of the page item
$pagePath = "/sitecore/content/Home/YourPage"

# Get the page item
$pageItem = Get-Item -Path $pagePath

# Get all image fields in the page item
$imageFields = $pageItem | Get-ItemField -ReturnType Field -Name "*.Image"

# Loop through each image field
foreach ($field in $imageFields) {
    # Get the image item
    $imageField = [Sitecore.Data.Fields.ImageField]$field
    $imageItem = Get-Item -Path "master:" -ID $imageField.MediaID

    # Create a new language version for the image item
    if ($imageItem -ne $null) {
        $language = "YourLanguageCode" # Specify the language code for the new version
        $existingVersion = $imageItem.Versions.GetVersions().Count -gt 0
        if (-not $existingVersion) {
            New-ItemLanguage -Item $imageItem -Language $language
            Write-Host "New language version created for $($imageItem.Name)"
        } else {
            Write-Host "Language version already exists for $($imageItem.Name)"
        }
    }
}

Your Answer

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