Is it possible to get a Sitecore item via Powershell script having only the public URL?
Something like an overload of the Get-Item function that receives the URL would be ideal
Thanks in advance
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityYou can get item by URL using below function. You may have to tweak it for resolving wildcard items.
function GetItemByUrl($url){
$url = [System.Uri]::new($url);
$siteContext = [Sitecore.Sites.SiteContextFactory]::GetSiteContext($url.Host, $url.PathAndQuery);
$homePath = $siteContext.StartPath;
if (!$homePath.EndsWith("/")){
$homePath += "/";
}
$itemPath = [Sitecore.MainUtil]::DecodeName($url.AbsolutePath);
if ($itemPath.StartsWith($siteContext.VirtualFolder)){
$itemPath = $itemPath.Remove(0, $siteContext.VirtualFolder.Length);
}
$fullPath = $homePath + $itemPath;
return $siteContext.Database.GetItem($fullPath);
}
$item = GetItemByUrl("http://localhost.sc/my-page")
Write-Host $item.ID