Sitecore Powershell script to move media items based on references

Pranay
May 16, 2020  ยท  4853 views

This post provides Sitecore powershell script to move media items based on the website in which these images are used in that Sitecore instance.

Scenario###

Say there are 3 websites in your sitecore instance, and the images/media items used by all these sites are placed in a single or multiple folders. Now if you want to reorganize all the existing media items based on the site in which they are used by placing them into separate folders in media library then this script is the one you need.

Is this not the script you are looking for? Then check the complete list of Sitecore Powershell scripts

multi sites - content

Note: If there are images which are used in multiple sites then either you can ignore them and let them be where they are or you may move them to a different folder say 'Common'.

multi sites - Media libary

Steps to reorganize the media items

  1. Create new folders in media library for each of the websites you have if they are not available already.
  2. Consider one website node each time. So update the website node path, destination path to move, Source path to search, excluded folder path or one site at a time.
  • $mediaExtensions - The type of media types you want to search and organize.

  • $brandNode - This is the website for which you are running the website.SO all the media items which are used by items under this site will be considered. As per my example - '/sitecore/content/Home/Site1'.

  • $excludeFromSourcePath - If you want to exclude a folder from searching, then this will be helpful '/sitecore/media library/Images/Site1'

  • $targetFolder - Target folder to which you want to move the items.

  • $sourcePath - The path where you want to search for. May be the entire media library folder or a 'Images' folder under media library. As in my example - '/sitecore/media library/Images'

  1. Run the script for each of the website separately.

Sitecore powershell script to reorganize the media items

Below is the script that needs to updated with the right inputs as per your needs and then ran.

$props = @{
   InfoTitle = "Referrers"
    InfoDescription = "Move media items based on the referrers"
    PageSize = 10000000
}

$mediaExtensions = "jpg", "png", "jpeg", "csv", "gif", "mp4", "pdf", "zip"
$sourcePath = "/sitecore/media library/Images"
$excludeFromSourcePath = "/sitecore/media library/Images/Site1"
$targetFolder ="/sitecore/media library/Images/Site1" # '/' is required at the end
$brandNode = "/sitecore/content/Home/Site1"; # '/' not required at the end

function Get-ItemReferrers {
    $items = Get-ChildItem -Path $sourcePath -Recurse | where-object { ($mediaExtensions -contains $_.Extension) -and ($_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder)}

    foreach($item in $items){
        if(!($item.ItemPath -match "^"+$excludeFromSourcePath+"\W") ){
        
            $linkDb = [Sitecore.Globals]::LinkDatabase
            $links = $linkDb.GetReferrers($item)
            $foundCount = 0
            $notFoundCount = 0
        
            foreach($link in $links){
                $linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID
            
                if($linkedItem.ItemPath -match "^"+$brandNode+"\W"){
                    $foundCount++
                }
                else
                {
                    $notFoundCount++
                }
            
            }
            #Write-Host "Matching:" $foundCount ", Not Matching:" $notFoundCount
            $matchingCount = ($foundCount).ToString() +  "/" + ($notFoundCount + $foundCount).ToString() + " matched"
            if( (($notFoundCount -le 0) -and ($foundCount -gt 0)))
            {
                #Write-Host "moving this item:" $item.ItemPath
                $customData = @{Matching = $matchingCount; Item = $item; Status = "Moved"}
                #Move-Item $item.ItemPath $targetFolder
                $customData
            }
            elseif ($notFoundCount -gt 0)
            {
                #Write-Host "Not moving this item:" $item.ItemPath 
                $customData = @{Matching = $matchingCount; Item = $item; Status = "Not moved"}
                #$customData
            }
        
        }
    }
}



$items = Get-ItemReferrers
$items | Show-ListView @props -Property @{Label="Name"; Expression={$_.Item.DisplayName} },
            @{Label="Extension"; Expression={$_.Item.Extension} },
            @{Label="Path"; Expression={$_.Item.ItemPath} },
            @{Label="References"; Expression={$_.Matching}},
            @{Label="Status"; Expression={$_.Status} }
Close-Window
AUTHOR

Pranay

A Software Engineer by profession, a part time blogger and an enthusiast programmer. You can find more about me here.


Post a comment




Thank you! You are now subscribed.

Sign up for our newsletter

Subscribe to receive updates on our latest posts.

Thank you! You are now subscribed.