I am trying to find a way to change the Properties (Alternate Text) of an image field in an item. Do you know how can I access those image properties using Sitecore Powershell?
2 Answers
Those properties are stored in the XML for the image field:
<image mediaid="{B4F11915-7EEF-4924-A8E4-B460D9929F59}" alt="Alt Text" height="168" width="300" hspace="" vspace="" />
You can easily update these by using the PSField.<fieldname>
object:
$item = Get-Item -Path "master:/content/home"
$item.Image = Get-Item -Path "master:\media library\Project\Website\General\test"
$item.PSFields.Image.Alt = "Set Some Alt Text"
$item.PSFields.Image.Width = 300
etc...
-
Thank you so much for sharing the XML format. I just simply used the code below to add the alt tag to the existing field and it worked for me. Mar 31, 2021 at 21:25
-
$newitem.Fields["Image"].Value = '<image mediaid="{ECF2E389-3F1F-49E6-8BC7-8F733B832653}" alt="' + $item.'Image alt' + '" Mar 31, 2021 at 21:26
You can use below script:
$mediaID = "mediaID"
Write-Progress -Activity $activity -Status "Checking if image exists" -PercentComplete $percentComplete -CurrentOperation $mediaID
$image = Get-Item -Path "master:" -ID $mediaID
if(!$image) { continue }
if([string]::IsNullOrEmpty($image.Alt)) {
Write-Host "Updating $($image.ItemPath)"
Write-Progress -Activity $activity -Status "Updating alt text for image" -PercentComplete $percentComplete -CurrentOperation $image.Alt
$image.Alt = "your Alt Tag text"
}
The scripts is from here: https://gist.github.com/michaellwest/b71095cca46bd4b5aae4f1085dac0966
-
1That script is going to change the media library item, not the properties of the image field on the page/datasource item.– Richard Seal ♦Mar 31, 2021 at 16:16