How to Create a Custom Exception Error Page in Sharepoint


While error handling in .NET applications and ASP.NET Web applications is fairly straightforward, But it  get quite complicated when dealing with solutions built with Microsoft Office SharePoint Server (MOSS) 2007 and Windows SharePoint Services. Because SharePoint comes out-of-the-box with its own custom error handling infrastructure (although it is based on the core functionality provided by ASP.NET).

1.gif

But if our  customer wants  a error page on Internet site with our custom Master Page and CSS applied, to provide a beautiful application. We should go for a custom Error Page.

2.gif

To implement a custom error page  for a share point site ,I have followed  two different approaches in two different projects.
  1. Write Custom Http Module to handle Error.
  2. Handle Error in Master page.
Write Custom Http Module to handle Error.

A)  Write a Http Module which will override the out of box Http Module .Handle the error over there and log the error  in Application log .Then it  redirect the user to the custom error page.You can use following code  from my class CustomExceptionHandler.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Manas.Common.Infrastructure.Exception;
using Manas.Common.Infrastructure.Logging; 

namespace Manas.ObjectModel
{
    /// <summary>
    /// This class help to handle Unhandle Exception by redirecting
    /// to a custom Error Page.
    /// </summary>
    class CustomExceptionHandler : IHttpModule
    {
        StringBuilder exceptionMsg = new StringBuilder("");
        public void Dispose()
        {
        }
        public void Init(HttpApplication context)
        {
            context.Error += new EventHandler(context_Error);
        }
        void context_Error(object sender, EventArgs e)
        {
            try
            {
                Exception[] unhandledExceptions = HttpContext.Current.AllErrors;
                foreach (Exception ex in unhandledExceptions)
                {
                    if (!string.IsNullOrEmpty(ex.Message))
                    {
                        exceptionMsg.Append(ex.Message);
                        exceptionMsg.Append("<br/>");
                    }
                }
                HttpContext.Current.Server.ClearError();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Redirect("/Pages/Error.aspx");
            }
            catch (Exception ex)
            {
                LogManager.WriteException(LogContext.UserInterface, ex);
                //UserInterfaceException UIex = new UserInterfaceException(ErrorCodes.ErrorInProductFlags);
                //throw (UIex);
            }
        }
    }
}

B) Then you need to create a Custom Error.aspx page and deployed it in appropriate place.

<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>

<%@ Page Language="C#" %>

<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

 <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

 <%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

 <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Error</title>   
</head>
<body>
    <form id="Form1" runat="server">
    <div class="container_12">
        <div id="masthead">
            <div id="logo">
                <a href="/" title="Go to site home page">
                    <img src="/_layouts/images/Krauta/logo.png" />
                </a>
            </div>
        </div>
        <div class="grid_12">
            <h1>
                Error</h1>
            <p>
                We apologize, but an error occurred and your request could not be completed.</p>
            <p>
                This error has been logged. If you have additional information that you believe
                may have caused this error, please contact technical support.</p>
        </div>
    </div>
    </form>
</body>
</html>

C) To activate custom unhandled exception error page Add  the following block in < httpModules> section of web.config.  ( in both default and internet zone site)

<httpModules>
      <clear />
     <add type="Manas.ObjectModel.CustomExceptionHandler,RELL.ObjectModel,Version=1.0.0.0, Culture=neutral,PublicKeyToken=c8490a3cc3a936e7" name="CustomExceptionHandler" />


Handle Error In Master page.

But the above approach some times difficult to handle .because in this case Server.Transfor not working .So I have used other approach in my current project

A)  First, we need to create our custom master page class  inherits from System.Web.UI.MasterPage .Specifically, in the page Init event, we wire up an event handler for the page Error event. and there we handle our Error as following instructions.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Manas.ObjectModel.SharePoint.Utilities
{
    [CLSCompliant(false)]
    public class  ManasMinimalMaster:System.Web.UI.MasterPage
    {
        private void Page_Error(object sender, EventArgs e)
        {
            Logger.LogError(this.Context.Error, this.Request.Url);
            this.Context.ClearError();
            this.Server.Transfer("/_layouts/MANAS/Error.aspx");
        }
        protected void Page_Init(object sender,EventArgs e)
        {
            // If an unhandled exception occurs, then SharePoint transfers the
            // request to /_layouts/error.aspx (via SPRequestModule). This
            // causes a couple of issues:
            // To avoid these issues, add a custom error handler for the page
            this.Page.Error += new EventHandler(Page_Error);
        }
    }
}

B) After create a custom MasterPage class.Create  your Custom _application.master file which is going to use all over the Share Point site.Your Custom _application.master must be inherits from your  CustomMinimalMaster.cs as following instructions.

<%@ Master Language="C#" Inherits="Manas.ObjectModel.SharePoint.Utilities.ManasMinimalMaster,Manas.ObjectModel.SharePoint, Version=2.0.0.0, Culture=neutral, PublicKeyToken=9j8b09h7d70e7ku9"%>

<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

 <%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


C) Create one Error page as specified in Above approach and deployee in appropriate place.

I think it wil help you to manage a custom Error page in share point application.

Up Next
    Ebook Download
    View all
    Learn
    View all