Custom Error/Message Application Pages in SharePoint 2010

Create a custom page that replaces a SharePoint application page. AccessDenied, Confirmation, Error, Login, RequestAccess, Signout, WebDeleted

In SharePoint 2010 it is possible to replace several application pages. This are: AccessDenied , Confirmation, Error, Login, RequestAccess, Signout, WebDeleted. This can be done in two ways. By using a eventreceiver or a powershell script.

public class AccessDeniedEventReceiver : SPFeatureReceiver
{
  const string CustomAccessDeniedPage =
                  "/_layouts/../AccessDenied.aspx";
  public override void FeatureActivated(
                         SPFeatureReceiverProperties properties)
  {
     if (properties != null)
     {
        SPWebApplication webApplication =
          properties.Feature.Parent as SPWebApplication;
        if (webApplication != null)
        {
           if (webApplication.UpdateMappedPage(
                 SPWebApplication.SPCustomPage.AccessDenied,
                 CustomAccessDeniedPage))
           {
             webApplication.Update();
           }
        }
     }
  }

  public override void FeatureDeactivating(
                         SPFeatureReceiverProperties properties)
  {
    if (properties != null)
    {
       SPWebApplication webApplication =
          properties.Feature.Parent as SPWebApplication;
       if (null != webApp)
       {
          if (webApp.UpdateMappedPage(
               SPWebApplication.SPCustomPage.AccessDenied,null))
          {
             webApp.Update();
          }
       }
    }
  }
}

Another way to do this is by using Powershell. The command Set-SPCustomLayoutsPage enables you to map a custom page. I.E.

Set-SPCustomLayoutsPage -Identity "AccessDenied" -RelativePath "/_layouts/custompages/accessdenied.aspx" -WebApplication "{replace with web app url}"

For more details on this command check MSDNExternal link icon

The eventreceiver has the advantage it is deployed with a feature, so you can deliver the custom page with that. The Powershell commands can be executed by an administrator and needs no code.

Share:
To Blog