5

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
}
4

Solution

Surround each of your expression part with

"$(EXPRESSION_HERE)"

Before:

@{Label="Redirect To"; Expression={ Get-PathFromId($_.Fields["Target Page"]) + $_.Fields["Target Url"] + Get-PathFromId($_.Fields["Parent Item"]) } }

After:

@{Label="Redirect To"; Expression={ "$(Get-PathFromId($_.Fields["Target Page"]))" + "$($_.Fields["Target Url"])" + "$(Get-PathFromId($_.Fields["Parent Item"]))" } }

Additional info

This trick is very useful if you want to include some more complicated expression inside your string. See following example.

$item =  Get-Item .

Write-Host "My path is $item.Paths.Path"
Write-Host "My path is $($item.Paths.Path)"

Output:

My path is Sitecore.Data.Items.Item.Paths.Path
My path is /sitecore/content/Home
  • Thanks @alan. Your solution makes sense but unfortunately doesn't work. I end up with no values. Before I was getting at least a value from the first field. – Jason Booth Jan 14 '17 at 17:47
  • Are you sure you are doing everything correctly? I never share solutions without checking. I created new templates with the same fields that you have and I was able to concatenate field values into single column. See dl.dropboxusercontent.com/u/191345630/Stecore/SE/… – Alan Płócieniak Jan 14 '17 at 18:27
  • Thanks, @Alan. I was doing it exactly as you suggested but your comment made me realize maybe it was an issue with data. Turns out I had null values that were causing errors in the Get-PathFromId method. I could see debugging that the method would exit unexpectedly. Doing null checks there and implementing your solution were the keys. Everything is working as expected now. Thanks! – Jason Booth Jan 16 '17 at 16:42
  • Could you please provide further information on the powershell concept you are using in this solution? – Jan Bühler Aug 16 '17 at 9:20
1

Another option you can consider is to use the Value property on the returned field.

Example: Access a field value using the Value property.

@{Label="Redirect To"; Expression={ Get-PathFromId($_.Fields["Target Page"]) + $_.Fields["Target Url"].Value + Get-PathFromId($_.Fields["Parent Item"]) } }

More on accessing fields here.

In doing more research, the same behavior can be experienced with the Select-Object command.

Example: The following returns no combined results.

$items | Select-Object @{"n"="Combined";e={$_.Fields["Title"] + $_.Fields["Title"] }}

Example: The following returns combined results.

$items | Select-Object @{"n"="Combined";e={$_.Fields["Title"].Value + $_.Fields["Title"].Value }}

Combined
---------
Sitecore Experience PlatformSitecore Experience Platform

Example: The same as above but with a function call.

$items | Select-Object @{"n"="Combined";e={(Get-PathFromId($_.Fields["Target Page"])) + $_.Fields["Title"].Value}}
  • Thanks @michael west. Unfortunately using .Value doesn't make a difference. However, I did notice that if I don't use my Get-PathFromId method from within the expression, then, using .Value does work. So, it has something to do with calling that method but unfortunately for me, I need to call that method for the report to display a path rather than a Sitecore item id. – Jason Booth Jan 14 '17 at 17:27
  • In that case, you'll want to add () around the function call. – Michael West Jan 14 '17 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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