Skip to content

Instantly share code, notes, and snippets.

@michaellwest
Created July 5, 2019 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaellwest/b71095cca46bd4b5aae4f1085dac0966 to your computer and use it in GitHub Desktop.
Save michaellwest/b71095cca46bd4b5aae4f1085dac0966 to your computer and use it in GitHub Desktop.
Update image alt tags using Sitecore PowerShell Extensions.
$rootPath = "/content/Company/DotCom/usa/home"
$file = "$($SitecoreDataFolder)\temp\alt-tags.csv"
$records = Import-Csv -Path $file
$totalCount = $records.Count
$currentCount = 0
$activity = "Updating alt tags"
foreach($record in $records) {
$currentCount += 1
$percentComplete = $currentCount / $totalCount * 100
$itemPath = $rootPath + ($record.Url -replace "www.Company.com","")
Write-Progress -Activity $activity -Status "Checking if item exists" -PercentComplete $percentComplete -CurrentOperation $itemPath
if(-not (Test-Path -Path "master:$($itemPath)")) { continue }
$page = Get-Item -Path "master:$($itemPath)"
$mediaID = $page.PSFields.HeadlineImage.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) -and ![string]::IsNullOrEmpty($record."Alt Tag")) {
Write-Host "Updating $($image.ItemPath)"
Write-Progress -Activity $activity -Status "Updating alt text for image" -PercentComplete $percentComplete -CurrentOperation $image.Alt
$image.Alt = $record."Alt Tag"
}
}
Write-Progress -Completed -Activity "Completed processing update"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment