Contact Us

Interactions are enhanced with graphical entities conveying the details. Most of the content and interface comprise a variety of media elements. And, to manage these media in Sitecore, there is Media Library.

You may manage every media entity, like images you wish to embed over the webpage or similar other applications requiring media display. To compile the Media Library, the PowerShell tool helps you easily code and execute it.

PowerShell is an exceptional tool for developers for task automation. It is typically available for systems that comprise a Command-line shell, scripting language, and management framework.

Moving forward into the Sitecore platform, there is PowerShell ISE (Integrated Scripting Environment) available in the development tools of the Sitecore interface. Here the flexibility scales up the testing and debugging of scripts. Let's understand how to compile Media Library using Sitecore PowerShell.

Use Sitecore PowerShell to Compile Media Library

  • Compile Unused Items

    Go to Desktop > Development tools > PowerShell ISE

    Image 1

  • Run the following script in the ISE

  • function HasReference {
    param(
    $Item
    )
    $linkDb = [Sitecore.Globals]::LinkDatabase
    $linkDb.GetReferrerCount($Item) -gt 0
    }
    function Get-ItemWithNoReference {
     $items = Get-ChildItem -Path "master:\sitecore" -Recurse |
       Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder
    }
      foreach($item in $items) {
       if(!(HasReference($item))) {
    $item
        }
       }
     }
    # Setup a hashtable to make a more readable script.
    $props = @{
    InfoTitle = "Unused items"
        InfoDescription = "Lists all items that are not linked to other items."
    PageSize = 25
        }
    # Passing a hashtable to a command is called splatting. Call Show-ListView to produce
    # a table with the results.
    Get-ItemWithNoReference |
    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} }
  • Result that appears
    Image 2
  • You can export the data into csv html or excel format
  • Check Alt tag on Images
  • Open PowerShell ISE and run the following script


  • $pathOfImages = "master:/sitecore/media library"
    $images = Get-ChildItem -Path $pathOfImages -Language * -Recurse |
    Where-Object { ($_.Fields["Alt"] -ne $null) -and ($_.Fields["Alt"].Value)  }

    if ($images.Count -eq 0) {
    Show-Alert "There are no images with an empty Alt field."}
    else {
    Write-Host -Foreground Cyan "$($images.Count) Images Found With
    Missing Alt Text"
    $props = @{
    Title = $PSScript.Name
    InfoTitle = "Images with an empty Alt field"
    InfoDescription = "Lists all images with an empty Alt field."
    PageSize = 25}
    $images |
    Show-ListView @props -Property @{ Label = "ItemID"; Expression = { $_.ID }
    },
    @{ Label = "ItemPath"; Expression = { $_.ItemPath } },
    @{ Label = "Name"; Expression = { $_.Name } },
    @{ Label = "AltText"; Expression = { $_.Fields["Alt"].Value } },
    @{ Label = "UpdateAltText"; Expression = { $_.Name -replace '[-_]',' ' } },
    @{ Label = "Language"; Expression = { $_.Language } }}

 

  • Finally, you will see the following results

Image 3

The above are the specific codes and commands and appropriate steps to compile the Media Library using the Sitecore PowerShell provided within the interface.

Further, once assembled, Media Library has its broader functioning. It manages your media entities throughout the web pages. Contact us for more Sitecore-related concerns or queries.

Need Help?