Saturday, March 20, 2010

SharePoint Custom Error Page

As you may already know SharePoint come out with "friendly" standard error page that does not give sufficient information about error that occurred. Since SharePoint web application presents ASP.NET 2.0 web application it should be easy to set custom error page by changing the web.config file. Unfortunately applying the changes will not take the effect as you expected like they would in pure ASP.NET 2.0 web application.

Fortunately there is an option to avoid seeing that standard SharePoint "friendly" error page by changing several settings in SharePoint web application web.config file. Depending on settings you may see the custom error page you've built or standard ASP.NET error page with full stack trace. During development it is very handy to see standard ASP.NET error page but from user perspective when it come to other environments (staging, acceptance, production) it is better to have error page that has the same look & feel as entire web application.

If you are building a SharePoint web application that is based on publishing site template than you will not be satisfied with out of the box error page since it will significantly differ from look & feel that you've built and also content on that page cannot be updated. For web applications that are based on team site template you will on the other hand usually keep the standard SharePoint error page because it fits in standard team site look & feel. It is important to mention that changing the look & feel of standard SharePoint error page should not be taken into consideration for several reasons, one is that changes will take effect on whole SharePoint server instance and all web applications that are created within that instance, second is any possible Microsoft update that can overwrite your custom changes.

Although this post is not about explaining of how to setup the standard ASP.NET error page still I must start my explanation with it because it is a good starting point for setting the custom error page and since most of the developers already know this.

Default configuration inside one SharePoint web application web.config file:

SafeMode
<SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
CustomErrors
<customErrors mode="On" >

In order to see the ASP.NET default error page with full stack trace apply next changes to appropriate SharePoint web application web.config file:

SafeMode
<SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
CustomErrors
<customErrors mode="Off" >

If you just change the CallStack to true you will see the standard ASP.NET error page but without exception details.

In order to see custom error page you've built (i.e. error.aspx) apply next changes to appropriate SharePoint web application web.config file:

SafeMode
<SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">
CustomErrors
<customErrors mode="On" defaultRedirect="/Pages/Error.aspx">

Conclusion is quite simple, in order to have the ability to define custom errors mode and default redirect as you already have with standard ASP.NET applications simply set CallStack on true and you will be able to configure your custom error page.

8 comments:

geeta said...

I tried to implement this for my sharepoint site. But redirect doesn't work. Give me the solution for this.

Boban Miksin said...

@geeta
Can you please give me more input, what have you done so far?

Paul said...

i also tried this and failed. I did exactly what you said to do. And it didnt work, can you please help?

Boban Miksin said...

@Paul
Have you created publishing portal, publishing page which presents the error page and then configure the web.config file? Please give me more input so I help you.

Bristol.NET said...

Just what I was looking for, thanks

Anonymous said...

Hi Boban,
I'm having problem with the redirect page - it won't go to my 'error.html' page.

Here is what I've in my Web Config and I saved my Error.html file in the same virtual director as the Web.config file is. Any suggestion would be appreciated.





Many thanks in advanced!

BlueSky

Boban Miksin said...

@BlueSky

SharePoint will not serve html pages from IIS folder because of the security policy or to be exact they will be served as empty pages. What you can do is to copy the html file to the 12 hive LAYOUTS folder and update the configuration defaultRedirect to /_layouts/error.html.

I cannot see the part of your configuration (I believe you haven't escaped xml before posting a comment) but it should look like this:

SafeMode

<SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">

CustomErrors

<customErrors mode="On" defaultRedirect="/_layouts/error.html">

However, I do not recommend you to take this approach. What you should do is to create new SharePoint page which will presents the error page and set it inside defaultRedirect, i.e. /Pages/Error.aspx.

Aman Kumar Bhardwaj said...

Hi boban,
This functionality is not working for moss. I have tried each & everything you have mentioned.
I believe the approach you have written is fully tested by you.
so could you please include the screen shots with steps.

thanks in advance.