Thursday, December 22, 2011

PowerShell - Set-SPCustomLayoutsPage to change the custom error page.

Goal

SharePoint site contains many out of the box Page layouts doing some important tasks. The list is mentioned below. Our goal is to change the Error page and point it to our custom ones. There are many help available on internet for this task but they are missing one thing in most of the blogs. The most important task is to recycle to IIS Application Pool after running the Set-SPCustomLayoutsPage. In this blog I have tried and kept a whole working example for the new comers to change the path and recycle IIS Application Pool using PowerShell.

  • AccessDenied (/_layouts/AccessDenied.aspx)
  • Confirmation (/_layouts/Confirmation.aspx)
  • Error (/_layouts/Error.aspx)
  • Login (/_layouts/Login.aspx)
  • RequestAccess (/_layouts/ReqAcc.aspx)
  • Signout (/_layouts/SignOut.aspx)
  • WebDeleted (/_layouts/WebDeleted.aspx)

Custom Error Page

Create a page "MySiteCustomError.aspx" on "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\" location. Paste the below given code.Use the below given body.
<html>
    <head>Error</head>
    <body>Page Not found</body>
</html>

Powershell

Create a PowerShell script with any "MySiteCustomError.ps1".
#MySiteCustomError.aspx - Custom error Page
#Error.aspx - Default error Page

write-host "Setup the custom Error Page"
Set-SPCustomLayoutsPage -Identity "Error" -RelativePath "/_layouts/MySiteCustomError.aspx" -WebApplication "http://sp2010:50000/"

# Function for recycling
[void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
function RecycleAppPools()
{
        $serv = new-object Microsoft.Web.Administration.ServerManager
 
        $serv.ApplicationPools | ? { $_.Name -like "SharePoint - 50000" } | % { $_.Recycle() }
}

write-host "Recycle the IIS"
RecycleAppPools

write-host "Script Completed!"


Test Case

  1. My Site > My Content > Blog
  2. I went to a blog and just changed an ID in querystring to a blog which was not there "http://sp2010:50000/personal/administrator/Blog/Lists/Posts/Post.aspx?ID=3". You can see the error which will pop up. This is the default error page in layouts.
  3. Run my "MySiteCustomError.ps1" in "Sharepoint 2010 Management Shell".(Note : Please change the URL and the Application Pool Name before running the script.)
  4. Refresh the "http://sp2010:50000/personal/administrator/Blog/Lists/Posts/Post.aspx?ID=3" and you can see the picture given below.

Hope this was much simpler and easier. Please leave me a message if you need any help.

1 comment:

  1. There is a Bug in SharePoint in setting up Custom Access Denied Page. To achieve this, please find the workaround in below URL

    Configure Custom Access Denied Page - BUG in SharePoint 2013 http://a2zdinesh.blogspot.in/2014/05/configure-custom-access-denied-page-bug.html

    ReplyDelete