1

I have an Image Field in an Item. When the image is uploaded to Media Library, the Alt field is not updated and hence when we look at the Image properties from Item, it is Blank. So far so good.

The content Author now has updated the Image's Alt Text and i can see that the "Default Alternate Text" is showing correctly as in screenshot below. But the content author is expecting the "Alternate Text" to be filled up with "Default Alternate Text". enter image description here

I planned to fill it with SPE. When i tried to browse the "Alternate Text" field from SPE, it shows an xml with MediaID without Alt tag.

$Item["Article Thumbnail"]

$Item.PSFields."Article Thumbnail"

For both, I see an xml as enter image description here

Now the question is how to fill "Alternate Text" from "Default Alternate Text". I can't access both fields from SPE. Any suggestions or solutions for this.

0

2 Answers 2

0

What you are trying to do is to update the raw value of the Image field.

To do this you need to get the target Image item from the raw value of the Image field and then get the Alternate Text of that image item. And then update the main item raw value to add alt text.

Sharing with you a sample script will help you to do this.

# Get the content Item by passing your item ID.
$item =  Get-Item -Path "master:" -Uri "sitecore://master/<your-item-id>" 

# Get the image field by passing your field name.
$imageField = [Sitecore.Data.Fields.ImageField]$item.Fields["<your-field-name>"]

# Get the actual Sitecore Image item by passing the mediaID (see your raw value)
$imageItem = Get-Item -Path master: -ID $imageField.mediaID

# Get the Alt value from the Image Item
$altValue = $imageItem.Fields["Alt"].Value

# Update the Main value 
$item.Editing.BeginEdit()
$item.Fields["Image"].Value = [string]::Format("<image mediaid='{0}' alt='{1}' />",$imageField.mediaID, $altValue)
$item.Editing.EndEdit()  

Now your raw value will look like this.

<image mediaid='{094AED03-02E7-4868-80CB-19926661FB77}' alt='Default Text' />

And you will be able to see it filled in your Image Properties. Hope this help.

enter image description here

5
  • Thank you for taking time and replying. Regarding you comments above, already the content author has updated the Alt text for the Image field. So why do we again need to update them? Whatever Alt Text is updated, when I open the Item's Image properties i see them under "Default Alternate Text" value. The requirement is to populate "Alternate Text" the same as Default Alternate Text's Value.
    – Yeshwanth
    Commented May 18, 2022 at 5:25
  • @Yeshwanth Did it resolve your issue? Commented May 18, 2022 at 5:27
  • No. There is no good luck.
    – Yeshwanth
    Commented May 18, 2022 at 5:35
  • @Yeshwanth I have updated my answer. Hope this will work. Commented May 18, 2022 at 6:46
  • This works. Thank you so much.
    – Yeshwanth
    Commented May 18, 2022 at 7:28
0

By using below code you can replace Default Alternate Text field value with Alternate Text field value under specific media items node.

$mediaTemplateName = "Image"
$mediaParentFolderPath = "master:/sitecore/media library/MediaFolder"
$counter = 1

$allMediaItems = Get-ChildItem -Path $mediaTemplateName -Recurse
$filteredMediaItems = $allMediaItems | Where-Object { $_.Fields["Alt"].Value -eq "" -and [Sitecore.Data.Managers.TemplateManager]::GetTemplate($_).InheritsFrom("Image") }

foreach($MediaItem in $filteredMediaItems) {
       
      $defaultAltValue = $MediaItem.Fields["Default Alternate Text"].Value
        #Update Alt tag

        $MediaItem.Editing.BeginEdit()
        $MediaItem.Fields["Alt"].Value = $defaultAltValue
        $MediaItem.Editing.EndEdit()

        Write-Host $counter " -> " $altValue " -> " $MediaItem.ItemPath
        $counter = $counter + 1
} 
1
  • Thank you for responding. When I tried the above script, i get an error Cannot index into a null array. $MediaItem.Fields["Default Alternate Text"].Value
    – Yeshwanth
    Commented May 18, 2022 at 5:30

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.