Ram Prasad Meenavalli

Mapping Custom Error Pages for SharePoint 2010 Site

Published on 18-Aug-2011 | Comments

We often create internet/intranet portals using SharePoint Server. In all the projects which I worked, one common requirement is to set custom error pages for the application. We were asked to display some custom error page instead of the default ERROR (HTTP STATUS - 500) page, ACCESS DENIED (HTTP STATUS - 401) page etc.,

In SharePoint 2007, we used to accomplish this requirement by implementing a HTTP Module, and we used to redirect to the appropriate pages based on the Http Status Code. Implementing this task in SharePoint 2010 is pretty easy now. The SPWebApplication object has a method called UpdateMappedPage which accepts two parameters. First one is an enum constant of type SPWebApplication.SPCustomPage which defines the type of error page to be mapped, and the second paramter is the URL of the custom error page. So, we can update many custom pages by calling this method of the SPWebApplication object. The SPWebApplication.SPCustomPage has the following values

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

So, using this method we can set custom error pages for all the above pages.
Lets see this in action. Follow the below steps

  • Create a new Project in Visual Studio and select 'Empty SharePoint Project' template.
  • Name the project as 'CustomErrorPages', and click on OK
  • Give the URL of your SharePoint site, and select 'Deploy as a farm solution' when asked for the deployment type
  • After the solution is created, right click on the project and add the "SharePoint Layouts mapped folder". This creates a mapped folder in the solution with a sub-folder named 'CustomErrorPages'
  • Right Click on this sub folder and click on Add->New Item.
  • From the templates, select 'Application Page'. And add the required content on this page with the desired UI
  • Now, right-click on the features folder and click 'Add Feature'
  • Make the scope of the feature as "WebApplication" and add a feature recevier for this
  • The FeatureActivated and FeatureDeactivating methods should have the code as shown below

public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; if (null != webApp) { if(!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, CustomErrorPage)) { throw new ApplicationException("Cannot create new error page mapping !!"); } webApp.Update(true); } } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; if (null != webApp) { if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, null)) { throw new ApplicationException("Cannot reset error page mapping"); } webApp.Update(true); } }

Deploy this solution on your SharePoint site. Install the feature using stsadm command or through powershell. Now type the url as 'http://siteurl/page~s/default.html'. This should open the custom error page which we created in our solution.
Note: If the Custom error page doesn't open, then re-install the feature, recycle the Application Pool and try again.

Using the above code we can map custom pages for other scenarios as well, by passing different SPWebApplication.SPCustomPage values to the UpdateMappedPage method.


View Ram Prasad Meenavalli's LinkedIn profile

SharePoint Technology Evangelist