0

I have an item where we have a "Article Thumbnail" field(Media type) and an image is inserted from Media Library.

Now I need to check for Alt text in Item's media properties. I am trying with below script and finding an odd behavior. When I browse for Item's field, I see an empty alt. But when I do selection for that field, it is going to its media library item and fetching me the alt. So, how to get correct Item's Image alt ?

enter image description here

To be more clear, see the highlighted part (Alternate Text and Default Alternate Text) enter image description here

1
  • Post your code as snippets instead of screenshots
    – Marek Musielak
    Commented Oct 5, 2022 at 12:33

2 Answers 2

2

You'll want to either cast the field to the proper field type or use the PSFields collection to access it for you.

Read more here.

Example: The following gets the home item of a site, accesses the HeroImage field and expands the Alt text.

$item = Get-Item -Path "master:" -ID "{961563FC-3445-4558-BF3A-06DF06BA6298}"
$item.PSFields.HeroImage.Alt

Example

Update

Based on your revised requirements here is what I've learned.

When accessing the Alt field on the ImageField type the text is first extracted from the xml in the field followed by a fallback to the text on the media item. Doesn't appear that the Alt property provides a way to know whether or not it's the default value.

You could try something like the following:

$item = Get-Item -Path "master:" -ID "{961563FC-3445-4558-BF3A-06DF06BA6298}"
$detectedValue = $item.PSFields.HeroImage.Alt
$defaultValue = $item.PSFields.HeroImage.MediaItem["Alt"]

if($defaultValue -ne $detectedValue) {
    Write-Host "This has been overridden"
} else {
    Write-Host "This is the default value"
}

$rawValue = $item.PSFields.HeroImage.GetAttribute("alt")
if($detectedValue -ne $rawValue) {
    Write-Host "It's showing the default value"
}

Example

"ABC" is stored in $rawValue and $detectedValue. If I cleared it out $detectedValue would match $defaultValue and $rawValue would be blank.

3
  • Thank you @Michael I have elaborated my question with another Image. In the Image, I need "Alternate Text" value and not "Default Alternate Text". The .PSFields.Alt is returning "Default Alternate Text" if Alternate Text is empty.
    – Yeshwanth
    Commented Oct 6, 2022 at 11:03
  • OH yeah, interesting dilemma. I updated my answer to include a solution for that. Commented Oct 6, 2022 at 21:33
  • Thank you! GetAttribute does all the trick here.
    – Yeshwanth
    Commented Oct 7, 2022 at 11:42
1

You can cast your item media field into ImageField in PowerShell and it will give you all the properties of this field. Here is the example code.

# Get the item using the ID
$item = Get-Item -Path "master:" -ID "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"

# Pass your field name. In the example below it is Image and cast into the ImageField
$imageField = [Sitecore.Data.Fields.ImageField]$item.Fields["Image"] 

You will get all the properties like this.

enter image description here

If your field doesn't have the alt value set then you could go to the Image Item using the mediaid and get the value from there. Here is the example code.

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

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

Hope this makes sense.

4
  • Thank you @Sumit. Please see my comment above replied for Michael West. My requirement is to get Alt value in item's media field and NOT Media's Alt from source.
    – Yeshwanth
    Commented Oct 6, 2022 at 11:06
  • I got your requirement after updating your Question. So it is good to get the Raw Value and get the attribute from there. Commented Oct 6, 2022 at 12:46
  • Use $imageField.GetAttribute("alt") to get the attribute. Commented Oct 6, 2022 at 13:05
  • Thank you! GetAttribute does all the trick here.
    – Yeshwanth
    Commented Oct 7, 2022 at 11:43

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.