I have created my first report, now I'd like to make it reusable. Is there any way I could read parameters from "somewhere" after the user clicks the report's name?
2 Answers
Currently there is no out of the box "settings" stored "somewhere" for SPE 4.1. Every solution is unique so it didn't make sense to build that. You can however create your own setting items and query that using the commands such as Get-Item
.
Have a look at the Authorable Reports module included. The Index Viewer contains an example of how to request information from the user.
Example: The following is a simplified version of the Locked Items report. Notice the use of the Read-Variable command.
$item = Get-Item -Path master:\content\
$props = @{
Title = "Items Locked"
Description = "This report will analyse the branch and will tell you which items havehave beenn locked."
Width = 450
Height = 300
OkButtonName = "Proceed"
CancelButtonName = "Abort"
Parameters = @(
@{ Name = "item"; Title="Root Item"; Tooltip="Branch you want to analyse."}
)
}
$result = Read-Variable @props
if($result -ne "ok") {
Close-Window
Exit
}
$items = Get-ChildItem -Path $item.ProviderPath -Recurse -Version * -Language * | Where-Object { $_.__Lock -and (-not ($_.__Lock -match "<r />"))}
$items |
Show-ListView -PageSize 25 -Property @{Label="Name"; Expression={$_.DisplayName} },
@{Label="Owner"; Expression={ $_.__Owner} },
@{Label="Locked"; Expression={ ([Sitecore.Data.Fields.LockField]($_.Fields["__Lock"])).Date} },
@{Label="Locked by"; Expression={$_.Locking.GetOwner() } },
@{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
The generic way would be to read user input, like Michael suggested.
Another way that we have used in some circumstances is to move the report script into a reusable function and put in a separate script item under Functions. You can then create light-weight "integration point" script items that import the shared function and call it with different sets of parameters.
Here is a simple example just to illustrate the idea:
Functions / Show-UsersReport
function Show-UsersReport {
param(
[string]$RoleName,
[switch]$IncludeAdministrators
)
# Reporting script here...
}
Toolbox / Show all users
Import-Function "Show-UsersReport"
Show-UsersReport -IncludeAdministrators
Toolbox / Show Authors
Import-Function "Show-UsersReport"
Show-UsersReport -RoleName "sitecore\Author"
Toolbox / Show Approvers
Import-Function "Show-UsersReport"
Show-UsersReport -RoleName "sitecore\Approver"
This is certainly not for every case, but has a few upsides, such as:
- Simplifies for users when only a few combinations of parameters are "valid"
- Allows each use case to have a descriptive name
- Allows separate security permissions for each integration point
- Allows separate rules for when each integration point should be visible/enabled (see the Interactive section fields for the script item)
Naturally, it is also possible for an integration point script to ask the user for to input if you need to leave some parameters open.