SharePoint

Custom Error/Message Application Pages in SharePoint 2010

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 MSDN

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 this

Leave a Reply

Your email address will not be published. Required fields are marked *