I'm working on an instance that has a custom redirect engine built in. I've been tasked to write a report that can show the redirect items' Sitecore path with the "redirect from" and "redirect to" fields.
There are 4 flavors of these redirects and the "redirect to" field has 3 different names across those 4 templates.
In my report, I am attempting to display those three field values in one column. The idea being that if I just concatenate those values together, 1 will have a value and 2 will be null.
function Get-PathFromId($item) {
$myItem = Get-Item master: -ID $item
$itemPath = $myItem.Paths.Path
$itemPath
}
function Get-TemplateName($template) {
$templateItem = Get-Item master: -ID $template.id
$templateName = $templateItem.DisplayName
$templateName
}
#get all redirects of each type
$items = Get-Item master: -Query "/sitecore/content//*[@@templatename='Internal Redirect Rule']"
$items += Get-Item master: -Query "/sitecore/content//*[@@templatename='External Redirect Rule']"
$items += Get-Item master: -Query "/sitecore/content//*[@@templatename='Redirect Rule']"
$items += Get-Item master: -Query "/sitecore/content//*[@@templatename='Route Redirect Rule']"
$props = @{
InfoTitle = "Redirects"
InfoDescription = "Lists all redirects for each region."
PageSize = 100
}
$items |
Show-ListView @props -Property @{Label="Path"; Expression={$_.ItemPath} },
@{Label="Redirect From"; Expression={$_.Fields["Source URL"]} },
@{Label="Redirect To"; Expression={ Get-PathFromId($_.Fields["Target Page"]) + $_.Fields["Target Url"] + Get-PathFromId($_.Fields["Parent Item"]) } },
@{Label="Internal Target"; Expression={ Get-PathFromId($_.Fields["Target Page"]) } },
@{Label="External Target"; Expression={ $_.Fields["Target Url"] } },
@{Label="Route Redirect"; Expression={ Get-PathFromId($_.Fields["Parent Item"]) } }
What I'm seeing is that the column where I am expecting concatenated values :
@{Label="Redirect To"; Expression={ Get-PathFromId($_.Fields["Target Page"]) + $_.Fields["Target Url"] + Get-PathFromId($_.Fields["Parent Item"]) } }
only returns the value from the "Target Page" field. Any values present in the other two fields are never displayed.
When I display the values individually (for now I've added them individually as the last three columns) they display values as expected.
I'm pretty green with SPE - this has to be something I don't understand about concatenation and/or calling that internal Get-PathFromId function within my concatenation.
Update
After working through possible solutions from Michael West and Alan Płócieniak, it seems that my issue is caused by calling the Get-PathFromId method from within my expression.
Using this expression without the method calls:
@{Label="Redirect To"; Expression={ $_.Fields["Target Page"].Value, $_.Fields["Target Url"].Value, $_.Fields["Parent Item"].Value } }
results in a fully populated column but the "Target Page" and "Parent Item" fields are drop links. Having the Sitecore Item Id in the report is not very useful for the intended audience.
Maybe there is a different way to get the Paths.Path from that value?
Update #2
Adam Płócieniak's solution ended up working great but I also had to fix the Get-PathFromId method to check null values. Some null values were wreaking havoc apparently but weren't throwing an error. I updated that method to:
function Get-PathFromId($item) {
$itemPath = ""
if($item -ne $null) {
$myItem = Get-Item master: -ID $item
if ($myItem -ne $null) {
$itemPath = $myItem.Paths.Path
}
}
$itemPath
}