Your problem is that Powershell is running in the context of the "shell" site, which gives you /sitecore/shell as the path. When using Sitecore.Links.LinkManager.GetItemUrl()
, you'll need to be in the context of the site for which you want the URL. For this, you can use the SiteContextSwitcher
, which can be used in a using block. In C#, you would do this as follows:
var site = Sitecore.Sites.SiteContext.GetSite("website");
using (var siteContextSwitcher = new SiteContextSwitcher(site)) {
var urlOptions = Sitecore.Links.LinkManager.GetDefaultUrlOptions();
urlOptions.AlwaysIncludeServerUrls = true;
urlOptions.ShortenUrls = true;
urlOptions.SiteResolving = true;
var url = Sitecore.Links.LinkManager.GetItemUrl(item, urlOptions);
}
In Powershell, you'll need to use the New-UsingBlock cmdlet:
function Get-ItemUrl($item){
$site = [Sitecore.Sites.SiteContext]::GetSite("website")
New-UsingBlock(New-Object -TypeName "Sitecore.Sites.SiteContextSwitcher" -ArgumentList $site) {
$urlOptions = [Sitecore.Links.LinkManager]::GetDefaultUrlOptions()
$urlOptions.AlwaysIncludeServerUrl = $True
$urlOptions.ShortenUrls = $True
$urlOptions.SiteResolving = $True;
$url = [Sitecore.Links.LinkManager]::GetItemUrl($item, $urlOptions)
return $url
}
}
However, if you're running this on a CM instance, the URL that you get now will use the hostname and scheme of the the site on the CM instance. Assuming you're looking for the URL for CD, you'll need to do some parsing, as the CM instance doesn't know the hostname and scheme for the CD instance.
function Get-ItemUrl($item){
$site = [Sitecore.Sites.SiteContext]::GetSite("website")
New-UsingBlock(New-Object -TypeName "Sitecore.Sites.SiteContextSwitcher" -ArgumentList $site) {
$urlOptions = [Sitecore.Links.LinkManager]::GetDefaultUrlOptions()
$urlOptions.AlwaysIncludeServerUrl = $True
$urlOptions.ShortenUrls = $True
$urlOptions.SiteResolving = $True;
$urlString = [Sitecore.Links.LinkManager]::GetItemUrl($item, $urlOptions)
$url = New-Object -TypeName System.Uri -ArgumentList $urlString
$absolutePath = $url.AbsolutePath
return "https://mysite.com" + $absolutePath
}
}
Notice that we still use AlwaysIncludeServerUrl
so that the URI we generate will be able to be parsed by the System.Uri
constructor. We then strip out the CM hostname and scheme, and replace it with the scheme and hostname for the CD instance (in this case, https://mysite.com
)
Note: There's no error handling currently. For example, if you don't have the scheme or hostname set on the site node, it won't generate a URL that can be parsed by the System.Uri
constructor. In this case, make sure that your site node is fully configured, or modify the code to make sure that the URL is valid and fully-qualified before you pass it to the System.Uri
constructor.
scheme="https"
on your site definition? Please edit your post to include the site definition from/sitecore/admin/ShowConfig.aspx
.scheme
, notschema
. This is a common mistake, because the two words are so similar.