Tuesday, March 20, 2012

How to customize SharePoint 2010 error pages

Many customers most of the time wants customize Sharepoint.
We know it and I write about it mostly.

So we are here for another one kind of customization:

Error Pages

We have many ways to customize error pages in SharePoint 2010.
The better way IMHO is to create a site feature and enable it when we create the sharepoint web application.

But what do we need to change the error pages when the web application is already created?

We can do it in two ways:

  • by a console application
  • by powershell

When I need to do this kind of works I like to use Powershell for many reasons: no project, compilation less, typeless, easy, smart, etc …

The class that can does this jobs is:

Microsoft.SharePoint.Administration.SPWebApplication

You can find it in the Microsoft.SharePoint.dll file.

The code explains by itself:





  1. clear 
  2.  
  3. $siteUrl = Read-Host -Prompt "Url Web Application: " 
  4.  
  5. if ($siteUrl.Length -ile 0) { 
  6.     $siteUrl = "http://win-7iu1bqtgj2n:17927/" 

  7.  
  8. $site = Get-SPSite $siteUrl -Limit All 
  9.  
  10. if (!$site) { 
  11.     return 

  12.  
  13. $spWebApp = $site.WebApplication 
  14.  
  15. if (!$spWebApp) { 
  16.     return 

  17.  
  18. try { 
  19.     # Output the newly assigned application page. 
  20.     $spWebApp.UpdateMappedPage([Microsoft.SharePoint.Administration.SPWebApplication+SPCustomPage]::Error, "/_layouts/MyPortal/error.aspx"
  21.     $spWebApp.UpdateMappedPage([Microsoft.SharePoint.Administration.SPWebApplication+SPCustomPage]::AccessDenied, "/_layouts/MyPortal/error.aspx"
  22.     $spWebApp.Update() 

  23. catch [System.Exception] { 
  24.     Write-Host $_ | fl * -Force 

  25.  
  26. Write-Host "Press any key..."
  27. Read-Host 

the most important lines of code are:


$spWebApp.UpdateMappedPage([Microsoft.SharePoint.Administration.SPWebApplication+SPCustomPage]::Error,"/_layouts/MyPortal/error.aspx")
$spWebApp.UpdateMappedPage([Microsoft.SharePoint.Administration.SPWebApplication+SPCustomPage]::AccessDenied, "/_layouts/MyPortal/error.aspx")


the UpdateMappedPage method writes in the SharePoint content database the information about the redirection in case of:



  • None
  • AccessDenied
  • Confirmation
  • Error
  • Login
  • RequestAccess
  • Signout
  • WebDeleted

bye Winking smile

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Fazio, im not sure if the above script worked for you, but this line [Microsoft.SharePoint.Administration.SPWebApplication].SPCustomPage::AccessDenied , should be
    [Microsoft.SharePoint.Administration.SPWebApplication+SPCustomPage]::AccessDenied to work. You may have to correct your post

    ReplyDelete