4

I'm working on some reports using Sitecore Powershell Extensions. One requirement is to provide the public URL for various Sitecore items. I'm thinking this isn't really possible via Link Manager because I don't have proper site context when running the report.

Is my thinking correct? Is there another non-hardcoded way to generate the URL in this scenario? Does the concept of context switching exist within PSE?

Probably important to note that this instance supports multiple sites so I can't assume a static domain for the URL.

UPDATE

After some Guidance from @Rirchard Seal and @Michael West below, here is what a function looks like to return a url:

function Get-UrlForItem($item) {
    $linkUrl = ""

    $siteContext = [Sitecore.Sites.SiteContext]::GetSite("website");

    New-UsingBlock(New-Object -TypeName "Sitecore.Sites.SiteContextSwitcher" -ArgumentList $siteContext) {
        $linkUrl = [Sitecore.Links.LinkManager]::GetItemUrl($item);
        $linkUrl
    }
}

Next up is to get the correct sitecontext for the item; right now I have it hard-coded to "website".

Thanks guys!

3
  • Do you know the site that the item in the report belongs too?
    – Richard Seal
    Commented Jan 18, 2017 at 18:11
  • 2
    Bear in mind that SPE (normally and hopefully only) runs on the CM instance, which may well have a different set of hostnames configured than your CD servers, so you may end up with the CM server URLs in your report.
    – jammykam
    Commented Jan 18, 2017 at 18:36
  • Were you able to change the 'UseDisplayName' property? I'm trying to have it to true in order to see the name depending on the site language and it is not working. Seems like the property is read-only. Commented Apr 4, 2018 at 18:47

2 Answers 2

7

With PowerShell you can use the New-UsingBlock cmdlet to mimick C#'s using statement.

So assuming that you can use ancestors of the item to get the Site Root and then get the SiteContext from that you could do this to get the link:

New-UsingBlock(New-Object -TypeName "Sitecore.Sites.SiteContextSwitcher" -ArgumentList $siteContext) {
    $linkUrl = [Sitecore.Links.LinkManager]::GetItemUrl($item);
}
5
  • + you can add 'urloptions' parameter to GetItemUrl for more options. And Initialize 'urloptions' like $urlop = New-Object ([Sitecore.Links.UrlOptions]::DefaultOptions) Commented Jan 18, 2017 at 18:32
  • Thanks @Richard Seal! Is Using-Object baked into PSE or do I have to define that as a function within my script? I've found a couple examples of it as a function - I added that to my script and it is not erroring but at this point my Url's are empty strings. Commented Jan 18, 2017 at 23:31
  • Should be part of PSE Iirc
    – Richard Seal
    Commented Jan 18, 2017 at 23:32
  • 1
    The command is New-UsingBlock. sitecorepowershell.gitbooks.io/sitecore-powershell-extensions/… Commented Jan 19, 2017 at 2:24
  • Thanks guys! Works like a charm - I added the full function as an update to my original post. Commented Jan 19, 2017 at 13:58
1

Just adding to this answer. I wanted to use something similar but on a list of items, thus, couldn't really use a using statement.

[Sitecore.Context]::SetActiveSite("Website")
$urlop = New-Object ([Sitecore.Links.UrlOptions]::DefaultOptions)
$urlop.AddAspxExtension = $true
$urlop.AlwaysIncludeServerUrl = $true
$items | select Name, @{Label="URL"; Expression={[Sitecore.Links.LinkManager]::GetItemUrl($_,$urlop)} }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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