0

I am completely new to Sitecore PowerShell script making. I have a task where I need to get the media item url from the specific folder sitecore/media library/somefolderofimages.

Media Item URL that I am talking about is formatted like this: sitecore/media library/somefolderofimages/cat.png, for example, Content Editors can get the script which will export all of the media items URL from some folder to the CSV File.

If anybody has any tips, please make sure to comment and give me tips on how to do it. Thank you in advance!

1 Answer 1

2

You can create a PowerShell report by which content authors can execute whenever needed. There are other ways to achieve the same results like adding a Context menu or a button in ribbon etc...

<#
    .SYNOPSIS
        Lists all media items in a folder
#>
#
# INPUT / CONFIGURATION
#
$props = @{
    Title = "Lists all media items"
    Description = "Lists all Media items"
    OkButtonName = "Run Report"
    CancelButtonName = "Cancel"
    Parameters = @(
        @{ Name = "mediaRootItem"; Title = "Media Item Root Folder"; Editor = "droptree"; Source = "/sitecore/Media Library/" }
        @{ Title = "Note"; Value = "Select the root (folder) of the Media Items."; Editor = "info" }
    )
}
$result = Read-Variable @props
if($result -ne "ok") {
    Close-Window
    Exit
}

function Get-MediaUrl($mediaitem) {
    $siteContext = [Sitecore.Sites.SiteContext]::GetSite("website");
    $result = New-UsingBlock(New-Object Sitecore.Sites.SiteContextSwitcher $siteContext) {
        [Sitecore.Resources.Media.MediaManager]::GetMediaUrl($mediaitem)
    }
    $result
}

#Start Executing the Report
Get-ChildItem -Path master: -ID $mediaRootItem.ID -Recurse | Where-Object { $_.TemplateName -ne "Media Folder" } | Show-ListView

Reference: https://sitecorechat.slack.com/archives/C09THADMX/p1638803732147400

PowerShell Script Results Window

2
  • Thank you for answering. One question. Is this doing the part for the CSV export?
    – Samke11
    Dec 12, 2021 at 20:23
  • This script will open the results in a window where content authors can download it as Excel or any other formats. There is an option in Sitecore PowerShell to convert the result as Excel. Dec 15, 2021 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.