Importing Media Library assets to Content Hub


An example PowerShell script to export Sitecore Media Library assets into Excel file, compatible with Content Hub Excel import format

Getting Started

Sitecore Content Hub DAM is gaining traction as a Digital Asset Management platform, far superior to the good old Sitecore Media library. Our clients who chose to adopt Content Hub DAM often have a sizeable Media Library, so it needs to be moved to Content Hub DAM. Content Hub has the ability to import pretty much any kind of entity, including Assets via Excel, which is great, but it took me some time to figure how, specifically the file format and then how to create such a file from Sitecore Media Library. Sitecore PowerShell Extensions is my go-to choice for this kind of scenario and below I'm providing the script to create an export file - feel free to tweak it and use it as you need.

One comment before we jump to the code: Content Hub comes with some pre-defined import settings, which in my case I had to update to allow a larger number of assets to be imported. More details on Excel import configuration can be found here.

PowerShell script to export Media Library Assets into Excel

# Your public site Url - it has to be public in order to be visible to Content Hub, so it can read assets from it 
$siteUrl = 'https://your sitecore CD url'
# Asset approval status defines wither assets whow up under assets or review or create pages in Content Hub
$lifeCycleStatus = 'M.Final.LifeCycle.Status.Approved'
$mediaItems = [System.Collections.Generic.List[psobject]]::new()

cd 'master:/sitecore/media library'

# Get every asset in Media Library (an item which is not folder) recursively. 
$itemsToProcess = Get-ChildItem -Recurse . | Where-Object { $_.TemplateName -ne "Media folder"}
if ($itemsToProcess -ne $null) {
    $itemsToProcess | ForEach-Object {
        # Do some Url massaging to turn it into a public Url of the asset
        $url = [Sitecore.Resources.Media.MediaManager]::GetMediaUrl($_) -replace '/sitecore/shell/', $siteUrl;
        $path = $_.Paths.FullPath -replace '/sitecore/media library/','' -replace $_.Name ,'' -replace '/',' '
        # below fields made sense in this particular case. Your choice of fields to include might be different
        $title = $_["Alt"];
        if ($title -eq '' -or $title -eq $null) {
            $title = $_.Name -replace '-',' ';
        $extension = $_["Extension"];
        if ($extension -ne '' -and $extension -ne $null) {
            $fileName = $_.Name + "." + $extension;
        } else {
            $fileName = $_.Name;
        $mediaItems.Add([pscustomobject]@{ItemName = $_.Name; `
                                                ItemId=$_.ID; `
                                                #Width = $_["Width"]; `
                                                #Heiht = $_["Height"]; `
                                                FileName = $fileName; `
                                                Description = $path.Trim() ` #$_["Description"]; `
                                                Title = $title; `
                                                Url=$url; `
                                                Updated=$_.Updated;  })
    # This will create output dialog, from which all results can be saved to multiple file formats, 
    # including Excel, which in this case is compatible with Content Hub's required import format  
    $mediaItems | Show-ListView -Title "Media Item URLs" -Property `
            @{ Name = "ItemName"; Expression = { $_.ItemName } },
            # Note this field, which I added to be able to later link Content Hub assets to their source items in Sitecore ML
            # This will come handy when we need to update all image and file fields from Media Library to Content Hub Urls
            # I'll code this second part in a separate post
            @{ Name = "ItemID"; Expression = { $_.ItemId } },
            @{ Name = "Title"; Expression = { $_.Title } },
            @{ Name = "File"; Expression = { $_.Url } },
            @{ Name = "Description"; Expression = { $_.Description } },
            @{ Name = "FileName"; Expression = { $_.FileName } },
            @{ Name = "FinalLifeCycleStatusToAsset"; Expression = {$lifeCycleStatus} }

Useful Links