Answered by:
Custom Accessdenied and Error page in SharePoint 2010

Question
-
All
I need to build a custom access denied page where if access is denied I can show user with a link to request access of with some text to call at this number. I am aware of how to build and deploy custom Application page but not sure how to call it. In another words I dont want to override the OOTB page in layout folder since its not recommended but in case of error (Error.aspx) and access denied I want to display a nice looking page and my custom message.
I saw this post which is fantastic http://www.zimmergren.net/archive/2010/01/05/sp-2010-how-to-event-receivers-and-custom-error-pages.aspx but he is doing this on event handler, in my case rquirement is more generic, there could be access denied at any monent and error can occur any time.
Any thoughts ?
Thanks in advance
Wednesday, August 10, 2011 6:23 PM
Answers
-
You can create custom HTTP Module and include in application web.config . Code will be like below. its not complete one but it will help you
using System;
using System.Web;
using Microsoft.SharePoint;
namespace Custom.ApplicationPages
{
public class CommonAccessDenied : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
/// <summary>
/// Method To init HTTP Module
/// </summary>
/// <param name="context">HttpApplication</param>
public void Init(HttpApplication context)
{
//Adding a PreRequestHandlerExecute event.
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
/// <summary>
/// Handling the PreRequestHandlerExecute event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
try
{
if (HttpContext.Current != null)
{
String Access="/_layouts/AccessDenied.aspx";
string url = HttpContext.Current.Request.Url.ToString();
if (url.Contains(Access.ToLower()))
{
HttpContext.Current.Response.Redirect("Custom Access Denied Page");
}
}
}
catch (Exception ex)
{
}
}
}
------------------------------------
------------------------------------
Rahul Sharma
------------------------------------
------------------------------------
Coding is all about passion !!!!!!
------------------------------------
http://sharepointarrow.blogspot.com/ ------------------------------------- Marked as answer by Shimin Huang Wednesday, August 17, 2011 11:32 AM
Wednesday, August 10, 2011 6:49 PM
All replies
-
You can create custom HTTP Module and include in application web.config . Code will be like below. its not complete one but it will help you
using System;
using System.Web;
using Microsoft.SharePoint;
namespace Custom.ApplicationPages
{
public class CommonAccessDenied : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
/// <summary>
/// Method To init HTTP Module
/// </summary>
/// <param name="context">HttpApplication</param>
public void Init(HttpApplication context)
{
//Adding a PreRequestHandlerExecute event.
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
/// <summary>
/// Handling the PreRequestHandlerExecute event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
try
{
if (HttpContext.Current != null)
{
String Access="/_layouts/AccessDenied.aspx";
string url = HttpContext.Current.Request.Url.ToString();
if (url.Contains(Access.ToLower()))
{
HttpContext.Current.Response.Redirect("Custom Access Denied Page");
}
}
}
catch (Exception ex)
{
}
}
}
------------------------------------
------------------------------------
Rahul Sharma
------------------------------------
------------------------------------
Coding is all about passion !!!!!!
------------------------------------
http://sharepointarrow.blogspot.com/ ------------------------------------- Marked as answer by Shimin Huang Wednesday, August 17, 2011 11:32 AM
Wednesday, August 10, 2011 6:49 PM -
Just came across this while I was searching for a reference myself. While a custom httpmodule is definitley a solution to this problem, its a more invasive approach than is necessary in SharePoint 2010. In 2010 now, there are APIs that provide simple redirects for all of the custom out of the box pages. You can basically map any of the out of the box application pages listed in this enumeration to your own custom application page which you would just provision typically using a Visual Studio WSP.
Here's some additional links describing the excercise. You can also map the custom pages using an out of the box PowerShell cmdlet, which I've provided a link to as well.
Resources:
http://todd-carter.com/post/2010/04/07/An-Expected-Error-Has-Occurred.aspx
And a reference to the PS cmdlet:
http://technet.microsoft.com/en-us/library/ff607768.aspx
I've done the httpmodule solution a couple times in 2007, where we had no choice. It works, but also consider that the httpmodule effectively executes some code with every request. This is obviously less than ideal since you'll be paying that cost but only actually needing it some very small percentage of the time (only when the error page you care about gets hit). When you configure the re-direct using the cmdlet or a feature receiver with the SP APIs, you only pay for the redirect logic when gets sent to the page that you override. I believe the SharePoint code basically just checks to see if you've configured an override for that Web Application, and if you have it performs a Server.Transfer to that page.
Hope this helps...
Jonathan Mast
Senior SharePoint Consultant | SharePoint911: SharePoint Consulting
Blog: www.jonathanpmast.com/blog
Twitter: @jonathanpmast- Proposed as answer by danWhite37 Wednesday, November 30, 2011 5:51 PM
Tuesday, August 23, 2011 1:23 PM -
I agree with Jonathan. HTTP Module may not be the most efficient way to have custome error pages.
Creating a custom page and then using powershell to change the required settings is definitely a better approach.
Regards, Mahesh
Tuesday, April 3, 2012 4:11 PM -
There is no 404 error page though in http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebapplication.spcustompage.aspxThursday, April 5, 2012 3:39 AM
-
404 requests won't get processed by SharePoint, so you have to do those a little bit different.
Refer to this KB article: http://support.microsoft.com/kb/941329
This question would have been worth posting in a different thread since "Error" and "AccessDenied" are different than 404 :)
Jonathan Mast
Senior SharePoint Consultant | SharePoint 911, a Rackspace Company: SharePoint Consulting
Blog: www.jonathanpmast.com/blog
Twitter: @jonathanpmastSunday, April 8, 2012 2:31 AM