I am looking for a solution to list items in the media library with file extensions pdf, doc
, and xls
followed by exporting to a csv.
-
There are two questions I think. For your Powershell ISE is not loading, I think you first need to resolve it. Are you getting any errors in logs when you run PowerShell? Can you try to install the PowerShell Package and then try? Maybe something is missing in the file or configuration. For the alternative solution, you can use C# code to get the data using Sitecore API.– Sumit BhatiaCommented Aug 21, 2022 at 15:44
3 Answers
There is an out of the box report you can use as an example:
By default it only provides image filtering but you could copy/extend to include other types.
Example: Here is the sample report in which you could enhance.
$sizeOptions = [ordered]@{
"100 KB" = 100000
"250 KB" = 250000
"500 KB" = 500000
"1 MB" = 1000000
"5 MB" = 5000000
"10 MB" = 10000000
}
$typeOptions = [ordered]@{
"gif" = 1
"jpg, jpeg" = 2
"pdf" = 3
"png" = 4
"svg" = 5
}
$settings = @{
Title = "Report Filter"
Icon = [regex]::Replace($PSScript.Appearance.Icon, "Office", "OfficeWhite", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
OkButtonName = "Proceed"
CancelButtonName = "Abort"
Description = "Filter the results based on the media size and type"
ShowHint = $true
Parameters = @{
Name = "selectedSize"
Value = 250000
Options=$sizeOptions
Title = "Larger Than"
Tooltip = "Filter the results for items larger than the specified size"
Editor = "combo"
}, @{
Name = "selectedTypeValues"
Value = 2,4
Options = $typeOptions
Title = "Media Extension"
Tooltip = "Filter the results for items with the specified extension"
Editor = "checklist"
Validation = { $_.Value -ne $null }
}
}
$result = Read-Variable @settings
if($result -ne "ok") {
exit
}
$selectedType = @()
foreach($val in $selectedTypeValues) {
switch($val) {
1 { $selectedType += "gif" }
2 { $selectedType += "jpg","jpeg"}
3 { $selectedType += "pdf" }
4 { $selectedType += "png" }
5 { $selectedType += "svg" }
}
}
$mediaItemContainer = Get-Item -Path "master:\media library"
$items = $mediaItemContainer.Axes.GetDescendants() |
Where-Object { $selectedType -contains $_.Fields["Extension"].Value -and [int]$_.Fields["Size"].Value -gt $selectedSize } |
Initialize-Item | Sort-Object -Property Size -Descending
$reportProps = @{
Title = "Media by size and type"
InfoTitle = "Media filtered by file size and extension"
InfoDescription = "Media found larger than $($selectedSize) bytes. Some paths included with a default installation were ignored."
Property = @("Name","TemplateName","Size", "Extension","ItemPath")
}
$items | Show-ListView @reportProps
Close-Window
You can update the script to filter for the additional file types. The dialogs are not required but would be helpful in making this report reusable. The final output can be exported to csv from the report view (Show-ListView
).
For your second question, you can write custom code in C#. For this, you need to call the get item method to get the media library and then need to apply a loop on the media library's children like below code:
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item mediaItems= master.GetItem("/sitecore/media library");
foreach(Sitecore.Data.Items.Item item in mediaItems.Children)
{
Sitecore.Data.Items.MediaItem media = new Sitecore.Data.Items.MediaItem(item);
//TODO: Check file extension like media.Extension
}
In the loop you can convert item into MediaItem
and then you can check extension.
I hope it will work.
You can use this script to export CSV.
$selectedType = @('pdf','doc','xls')
$mediaItemContainer = Get-Item -Path "master:\media library"
$items = $mediaItemContainer.Axes.GetDescendants() |
Where-Object { $selectedType -contains $_.Fields["Extension"].Value } |
Initialize-Item
$items | Select-Object ID, FullPath | Export-Csv -notypeinformation -Path "C:\temp\files.csv"
Write-Host 'Script ended'
I copied script from Script Library in Sitecore /sitecore/system/Modules/PowerShell/Script Library/SPE/Reporting/Content Reports/Reports/Media Audit/Media items by size and type
which is same as what @Michael posted. I added only csv export part.