Custom Error and Access denied pages in Sharepoint

Ok, lets get started.

So for my latest project I had to replace the default sharepoint error pages with a custom one. There are different types of error pages such as the ones for 404 and 500 errors. These are set by IIS and are basically simple html pages that are shown if the requested page is not found or other errors.  But the error page that sharepoint uses is shown for errors that relate to sharepoint and shows correlation id and a simple message.

The default page for this type of error is located in the 14 hive /template/layouts/error.aspx.

Error.aspx inherits from Microsoft.Sharepoint.ApplicationPages.ErrorPage and by examining this in Reflector it’s relatively easy to see how it works. When an error occurs, the Item collection in the HttpContext gets filled by some keys such as ErrorText and ErrorCorrelationId. The error page simply gets these strings from the context (and also the query string) and renders them.

So, to make a custom error page is not harder than making a default application page with some code behind. You could of course just take the default error.aspx and design the markup as you want and use it as it is.

Alright, so you have a newly designed error page that you want to deploy to your sharepoint site. Well this is not harder than making a WebApplication scoped feature and adding a feature receiver with some custom code like this:

public class CustomErrorPageEventReceiver : SPFeatureReceiver 

 const string CustomErrorPage = "/_layouts/Contoso.Portal/ContError.aspx"; 
 public override void FeatureActivated(SPFeatureReceiverProperties properties) { 
 var webApp = properties.Feature.Parent as SPWebApplication; 
 if (webApp != null) { 
 webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, CustomErrorPage); 
 webApp.Update(true); 


} 

Ok, activate your feature and throw an error.

You can use the same feature and similar piece of code for more pages such as access denied as mentioned.

Advertisements

2 thoughts on “Custom Error and Access denied pages in Sharepoint

  1. Pingback: Custom Error and Access denied pages in Sharepoint | Bool Tech

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s