XMC: Open Page in Browser

1 min read

In this article I will explain how to open a page in the browser using Sitecore PowerShell Script XMC.

While working on the XMCloud project, I came across a situation where I needed to open a page in the browser. However, due to the limitations or we can say future vision of the XMCloud, I could not find a way to do it by extending the Sitecore pipeline architecture.

Hence, I decided to write a script that would do the job.

With some research and a bit of trial and error, I was able to write a script that would open a page in the browser.

The following is the script you can refer.

xmc-open-page-in-browser.ps1
$item = Get-Item .
$path = $item.Paths.FullPath
$siteInfo =   [Sitecore.Links.LinkManager]::ResolveTargetSite($item)

$scheme = $siteInfo.Scheme
if($scheme -eq $null -Or $scheme -eq "")
{
     $scheme = "https"
}
$siteBaseUrl = $scheme + "://" + $siteInfo.TargetHostName
if (-not $siteBaseUrl.EndsWith("/")) {
    $siteBaseUrl = $siteBaseUrl + "/"
}

$options = [Sitecore.Links.UrlOptions]::DefaultOptions
$options.AlwaysIncludeServerUrl = $true
$options.LanguageEmbedding = [Sitecore.Links.LanguageEmbedding]::Never
$options.LowerCaseUrls = $true
$itemUrl = [Sitecore.Links.LinkManager]::GetItemUrl($item, $options)
Invoke-JavaScript -Script "window.open('$itemUrl');"

Close-Window