Monday, August 1, 2011

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 type Microsoft.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(). To solve that issue please visit on my other post http://www.anmolrehan-sharepointconsultant.com/2011/08/access-denied-error-on.html

4 comments:

  1. How is "sign in as different user" work in this case? I assume that functionality is there in default access denied page and as soon i got my custom page created i am loosing that functionality. what's the solution for that?

    ReplyDelete
  2. You can inherit from the "AccessDeniedPage" in "Microsoft.SharePoint.ApplicationPages" assembly. This page handles this functionality.

    ReplyDelete
  3. Thanks Anmol for the tip! This saved my day.

    I am still getting an issue: after following your instructions I can see that the AccessDenied page is set correctly by checking iwth the following command:

    Get-SPCustomLayoutsPage -Identity "AccessDenied" -WebApplication "http://webapp"

    However when I try to access the web application I get a "403: Forbidden" error. Is there something I missed?

    ReplyDelete
  4. This is working in SP 2010 but not in SP 2013. Any tips/tricks to make it working in SP 2013.

    ReplyDelete

Popular Posts