Search This Blog

Tuesday, February 24, 2015

How to use Custom Access Denied Page in SharePoint 2010?


I came across a situation where I need to replace default Access Denied Page of Sharepoint with my own custom Access Denied Page.
For this there is a method available in SPWebApplication class named 'UpdateMappedPage'. This method takes two parameters.
  1. The application page to replace with the specified custom application page. This is an enum of typeMicrosoft.SharePoint.Administration.SPWebApplication.SPCustomPage and with available values AccessDenied (Specifies AccessDenied.aspx)
     Confirmation (Specifies Confirmation.aspx)
     Error (Specifies Error.aspx)
     Login (Specifies Login.aspx)
     RequestAccess (Specifies ReqAcc.aspx)
     Signout (Specifies SignOut.aspx)
     WebDeleted (Specifies WebDeleted.aspx)
  2. The location of the custom application page. This must start with "/_layouts/".  If want to reset back to default then pass this as null.

Steps to make it working.

  1. Create empty Sharepoint Project.
  2. Create your costom Access Denied Page in Layouts e.g
    /_layouts/MyCustomLayout/AccessDenied.aspx
  3. Create a Feature and set scope to site. Add event receiver to this feature.


[Guid("ffcbe7bb-4093-4c39-953d-de07236c878c")]
    public class ReplaceDefaultPagesFeatureEventReceiver : SPFeatureReceiver
    {
        const string customAccessDeniedPage = "/_layouts/MyCustomLayout/AccessDenied.aspx";
       
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite _site = new SPSite(site.ID, SPUserToken.SystemAccount))
                {
                    SPWebApplication webApp = _site.WebApplication;
                    if (null != webApp)
                    {
                        if (webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied, customAccessDeniedPage))
                        {
                             webApp.Update();
                        }
                    }
                }
            });
        }


        //On deactivate set default access denied page
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite _site = new SPSite(site.ID, SPUserToken.SystemAccount))
                {
                    SPWebApplication webApp = _site.WebApplication;
                    if (null != webApp)
                    {
                        if (webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied, null))
                        {
                              webApp.Update();
                        }
                    }
                }
            });
        }
    }

You may receive access denied error on webApp.Update().

No comments:

Post a Comment