Saturday, January 20, 2018

Sitecore Powershell :: Reporting 'used image items with empty Alt field' using Alt tag Action

My client came up with a requirement to get the list of Media items with empty Alt field. In our existing Powershell package, we already have such reports.

But, when the requirement got refined to get the list of Used Media items with empty Alt Field (Because we have junk of media items which were unused) there were no options.
As a result,  I created the below scripts to get Used media items with Empty Alt field.
Create a Script under the path (/sitecore/system/Modules/PowerShell/Script Library/Content Reports/Reports/Media Audit/Used Image items with empty Alt field)




function HasReference {
    param(
        $Item
    )
    
    $linkDb = [Sitecore.Globals]::LinkDatabase
    $linkDb.GetReferrerCount($Item) -gt 0
}

function Get-MediaItemWithNoReference {
    $items = Get-ChildItem -Path "master:\sitecore\media library" -Recurse
 | Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder }
    
    foreach($item in $items) {
        if((HasReference($item))) {
   if($item."Alt" -eq '') {
            $item
        }
      }
    }
}

$items = Get-MediaItemWithNoReference

if($items.Count -eq 0) {
    Show-Alert "There are no used media items."
} else {
    $props = @{
        InfoTitle = $PSScript.Name
        InfoDescription = "Lists used media items that are not linked from other items."
        PageSize = 25
        Title = $PSScript.Name
    }
    
    $items |
        Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },
            @{Label="Updated"; Expression={$_.__Updated} },
            @{Label="Updated by"; Expression={$_."__Updated by"} },
            @{Label="Created"; Expression={$_.__Created} },
            @{Label="Created by"; Expression={$_."__Created by"} },
            @{Label="Path"; Expression={$_.ItemPath} }
}
Close-Window


After running the script, I extracted the report with empty Alt tags.
But then I thought how they will be able input those bulk data from the report.
So, instead of opening each item, I came up with an idea to give them the Alt field against each media item. Hence, I created the Alt Tag Action under Item Ribbon.
(/sitecore/system/Modules/PowerShell/Script Library/Platform/Internal/List View/Ribbon/Item/Alt Tag)



foreach($Item in $selectedData)
{
    
$mediaItem = [Sitecore.Data.Items.MediaItem]$item;
$alt = $mediaItem.Alt;
$result = Read-Variable -Parameters `
   @{ Name = "alt"; Title = "Alt Tag";  Columns = 6 }   `
   -Description "Edit alt tag of '$($Item.DisplayName)'" `
   -Title "$($Item.DisplayName)" -Icon "Applications/16x16/photo_scenery.png"
                -Width 400 -Height 470 -OkButtonName "Change"
                -CancelButtonName "Cancel" -ShowHints;
  if ($result -eq "ok") {
  $Item.Editing.BeginEdit()
    $Item.Fields["Alt"].Value = $alt
    $Item.Editing.EndEdit()

 }
}


But this action was applicable for all the items, now what to do ??
Simple, restrict the rules only for Media items as below and it’s done ️.




Run the script now.







Select the media Item and Click Alt Tag Action.




Click change and open the Media item for the verifying the Input.



Download the Sitecore Package: Alt Tag Powershell Package

No comments:

Post a Comment