Fix Publishing Pages Page Layout URL error – Value does not fall within the expected range

While accessing publishing pages, you may encounter error –  “Value does
not fall within the expected range” . It happens due to one of the following reasons:
  • If the publishing layout URL of a publishing page contains an incorrect top level site URL.
  • If there is a problem with the Page Layout, or if the Page Layout is corrupted.
  • If a content database was moved or a content deployment job was run.
 
If you encounter this issue due to reason – 1 i.e. “If the publishing layout URL of a publishing page contains an incorrect top level site URL.”, you can fix it by executing following script.
 
It will fetch URL from Page Layout and verify if it is same as top level site URL. If not, it will change it to top level site URL and approve the page.
 
function AutoApprovePage($page)
{
    if($page.ListItem.ListItems.List.EnableModeration)
    {
        #Check to ensure page requires approval, and if so, approve it
        if ($page.ListItem[“Approval Status”] -eq 0)
        {
             write-host ” | Already approved:”, $page.Title -nonewline
        }
        else
        {
            $page.ListItem.File.Approve(“Page approved automatically by PowerShell script”)
             write-host ” | Approving:”, $page.Title -nonewline
        }
    }
    else
    {
         write-host ” | No approval required for: “, $page.Title
    }
}
 
CLEAR
$site = Get-SPSite -Identity “http://br203:50000”
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
Write-host “Processing SPWeb: “$pubWeb.Url
Write-host “Title: “$pubWeb.Title
       
$pagesList = $pubWeb.PagesList
$pageCount = 0
$badUrlCount = 0
$fixUrls = $true
foreach($page in $pagesList.Items)
{
    $curPubPage = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($page)
    $pageLayout = $curPubPage.ListItem.File.Properties[“PublishingPageLayout”]
    $url = $pageLayout.SubString(0, $pageLayout.IndexOf(‘,’))
 
    $pageCount += 1
    if(!$url.StartsWith($site.RootWeb.Url,[System.StringComparison]::OrdinalIgnoreCase))
    {
        Write-Host ” |-Page:”$page.Title
        Write-Host ”   |-Layout:”$url -nonewline
        if($fixUrls)
        {
            #Construct the path to page. First, get the last index of / to getthe page name
            $pos = $url.LastIndexOf(‘/’)
            if($pos -ne -1)
            {
                $pubSPFile = $curPubPage.ListItem.File
                $rootWebUrl = $site.RootWeb.Url
                $pageName = $url.SubString($pos + 1)
                $newPageLayout = $rootWebUrl + “/_catalogs/masterpage/” + $pageName
                $pageLayoutFile = $site.RootWeb.GetFile($newPageLayout)
                if($pageLayoutFile.Exists)
                {
                    $checkedOutToUser =  $pubSPFile.CheckedOutByUser
                    if(($pubSPFile.CheckOutStatus -ne “None”) -and $undoCheckOut)
                    {
                        Write-host ”   |-File is Checked Out to: “$checkedOutToUser.Name”, but will be overridden” -foregroundcolor blue -nonewline
                        $pubSPFile.UndoCheckOut()
                    }
                    if($pubSPFile.CheckOutStatus -eq “None”)
                    {
                        $pubSPFile.CheckOut()
                        # Save the page layout as “url, pagename” (with a single space after url, this is important)
                        $propVal = $newPageLayout + “, ” + $pageName
                        $pubSPFile.Properties[“PublishingPageLayout”] = $propVal
                        $pubSPFile.Update()
                        $pubSPFile.CheckIn(“Fixed page layout url via auto-script.”,[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn);
                        AutoApprovePage($curPubPage)
                        $fixedUrlCount += 1
 
                        Write-Host ”   |-SetUrl:”$newPageLayout -nonewline
                        Write-Host ” (Fixed)” -foregroundcolor Green -nonewline
                    }
                    else
                    {
                        Write-host ”   |-Not fixed. File is Checked Out to: “$checkedOutToUser.Name $checkedOutToUser.Email -foregroundcolor blue -nonewline
                    }
                }
                else
                {
                    Write-Host ”   |-Not fixed. The page layout $pageName does not exist in the root web master page gallery.” -foregroundcolor blue -nonewline
                }
            }
        }
    }
}
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s