Tuesday, August 9, 2011

MOSS - Custom error page

In MOSS 2007, how will you display a custom error page?
By using the simple ASP.net way of setting <custom errors="On" defaultRedirect="errorURL"/> won't work in sharepoint

You basically need to write a custom HTTPModule and add the entries in the web.config of your webapplication.

Here is a sample for your reference.

public class MyError : IHttpModule
        {
            const string errorUrl = "/_layouts/CustomErrorPage/Error.aspx";

            public void Dispose()
            {
                //dispose your objects
            }

            public void Init(HttpApplication context)
            {
                context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);

            }
            void context_PreRequestHandlerExecute(object sender, EventArgs e)
            {
                Page page = HttpContext.Current.CurrentHandler as Page;
                if (page != null)
                {

                    page.Error += new EventHandler(page_Error);
                }
            }


            void page_Error(object sender, EventArgs e)
            {
                Page page = sender as Page;
               
                    string strWebURL = SPContext.Current.Web.Url;
                    HttpContext.Current.Response.Write("<script type=\"text/javascript\">window.location = \"" + errorUrl + "\"</script>");
                    HttpContext.Current.Response.Flush();
               
            }
        }

Hope you all know how to deploy it. If you need steps on how to deploy, please feel free to ask.


Happy Coding :)


No comments:

Post a Comment