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!

  • Do you know the site that the item in the report belongs too? – Richard Seal Jan 18 '17 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 Jan 18 '17 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. – Marc Autumns Apr 4 '18 at 18:47
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);
}
  • + you can add 'urloptions' parameter to GetItemUrl for more options. And Initialize 'urloptions' like $urlop = New-Object ([Sitecore.Links.UrlOptions]::DefaultOptions) – Balaji Kuppuswamy Jan 18 '17 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. – Jason Booth Jan 18 '17 at 23:31
  • Should be part of PSE Iirc – Richard Seal Jan 18 '17 at 23:32
  • 1
    The command is New-UsingBlock. sitecorepowershell.gitbooks.io/sitecore-powershell-extensions/… – Michael West Jan 19 '17 at 2:24
  • Thanks guys! Works like a charm - I added the full function as an update to my original post. – Jason Booth Jan 19 '17 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, privacy policy and cookie policy

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