I implemented a custom 500 error page for a SharePoint 2010 publishing website.

The feature receiver of a web application scoped feature sets the custom error page as follows:

var webApplication = properties.Feature.Parent as SPWebApplication;
if (!webApplication.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, _customErrorPage))
    throw new ApplicationException("Could not set custom error page.");

In the error page itself, I'd like to log the unhandled exception. However, Context.Server.GetLastError() always evaluates to null.

Some research showed that perhaps I should set the redirectMode attribute of customErrors in my web.config file to ResponseRewrite - but that didn't resolve the issue.

ASP.NET developers seem to get around thing by using the Application_Error handler in Global.asax.cs to store the error in order to be able to access it later. We don't have that luxury in SharePoint.

Any suggestions on how to get around this issue?

Thank you

1 Answer 1


If you have .NET reflector, check out the way SharePoint handles this stuff with its own error page.

Specifically, look at SPUtility.TransferToErrorPage in the Microsoft.SharePoint.dll, and the Microsoft.SharePoint.ApplicationPages.ErrorPage code behind class in the Microsoft.SharePoint.ApplicationPages.dll

You'll see that SharePoint actually clears the error from HttpContext.Server, and storing the error info in the HttpContext.Items collection before doing a server.transfer.

eg the error text is stored in context.Items["ErrorText"]

  • Paul, thanks for the tip. The ErrorText in the context always appears to be "An unexpected error has occurred", not the true underlying exception message. Commented Nov 29, 2011 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.