Home » Customization » Create a Custom Report in Sitecore PowerShell Extensions

Create a Custom Report in Sitecore PowerShell Extensions

Sitecore Technology MVP 2016
Sitecore MVP 2015
Sitecore MVP 2014

Enter your email address to follow this blog and receive notifications of new posts by email.

During my Sitecore PowerShell Extensions presentation at the Sitecore User Group Conference 2014, I showcased a custom report I had scripted using the Sitecore PowerShell Extensions module, and thought I would jot down what I had shown coupled with some steps on how you could go about creating your own custom report.

I had shown the audience the following PowerShell script:

<#
    .SYNOPSIS
        Lists all images with an empty Alt field.
    
    .NOTES
        Mike Reynolds
#>

function Get-ImageItemNoAltText {    
    $items = Get-ChildItem -Path "master:\sitecore\media library\images" -Recurse | Where-Object { $_.Fields["Alt"] -ne $null }
    
    foreach($item in $items) {
        if($item."Alt" -eq '') {
            $item
        }
    }
}

$items = Get-ImageItemNoAltText

if($items.Count -eq 0) {
    Show-Alert "There are no images with an empty Alt field."
} else {
    $props = @{
        InfoTitle = "Images with an empty Alt field"
        InfoDescription = "Lists all images with an empty Alt field."
        PageSize = 25
    }
    
    $items |
        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} }
}
Close-Window

I modeled the above script after the “out of the box” ‘Unused media items’ report but made some changes: it grabs all media library items recursively under /sitecore/Media Library/Images — you could definitely change this to /sitecore/Media Library to get all images outside of the Images folder — in Sitecore that have an Alt field, and that Alt field’s value is equal to the empty string.

I then tested — yes, I do test my code, don’t you 😉 — and saved my report using the PowerShell ISE:

custom-spe-report-images-no-alt-text

The report was saved in this Item created just for it:

custom-spe-report-images-no-alt-text-location

Let’s see this in action!

I went to Sitecore –> Reporting Tools –> PowerShell Reports –> Mikes Media Audit, and clicked on the new report:

went-to-custom-report

After running the report, I was presented with this dialog containing the results:

images-with-no-alt-text-report-results

I then clicked on the first row of the report, and was brought to an image with an empty Alt field:

go-to-an-image-no-alt-text

If you have any thoughts on this, or would like to see additional reports in Sitecore PowerShell Extensions, please share in a comment.


9 Comments

  1. That’s fantastic! Love the article!
    One thing I think we could utilize more is the “-Title” attribute of the “Show-ListView” commandlet to give your report window a meaningful name. Regular user doesn’t really care that the report is a “PowerShell Script Result” and it also looks better in the Desktop taskbar.
    An easy extension to this would be a a test of context – if your function tested whether “Get-location” is within the media library you could easily save it under “content editor -> context menu ” and audit parts of your tree!
    Great report though! I hope you’ll add i to the OOTB SPE reports!

  2. Wow, that looks quite useful! I have some ideas for some custom reports I will try with this.

  3. […] PS. Funny thing – even though I’ve commented on Mike’s blog when he originally wrote about about it I’ve completely forgotten about it at the time of writing this blog. You can read more about the subject on Sitecore Junkie. […]

  4. […] Create a Custom Report in Sitecore PowerShell Extensions (this time by Mike Reynolds) […]

  5. Mickey Rahman says:

    Hi Michael,

    Great stuff – looks very powerful. I have a question, when a function gets called such Get-ChildItem, how does it go and find the data? is it doing Db lookups, xpath queries or lucene index lookups?

  6. I just had a look at the code for the Get-ChildItem command — this lives in the Cognifide.PowerShell.PowerShellIntegrations.Provider.PsSitecoreItemProvider class in Cognifide.PowerShell.dll — and it’s calling the GetChildren() method on the Item that is passed.

  7. Mickey, by default it will get the item by path. When you’re starting ISE or Console your path will be set in a context of a Sitecore provider so you can do things like Get-ChildItem .
    And it will work on paths by default. But Get-Item also has another mode where you can provide Sitecore Query as a -Query parameter or item ID in the -ID parameter.

    For more details please refer to this post: http://blog.najmanowicz.com/2014/10/12/working-with-sitecore-items-in-powershell-extensions/

  8. I downloaded and installed the PowerShell Extensions 2.8 on my local sitecore 6.5 instance.

    When I try to run your report (or any other report), i get the error message “Method not found: Boolean Sitecore.Handle.get_IsLocal()”.

    Does anyone know what this error means (I tried googling this error but didnt find anything).

    • I installed PowerShell Extensions 2.4 and that worked with my local sitecore 6.5 instance and I no longer get the get_IsLocal method error. Just wanted to post here so others dont run into this issue.

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.