SharePoint2010 Replace AccessDenied.aspx, Confirmation.aspx and other page with our own custom page



In sharePoint2010, we can replace the list of the following page with our own custom page.
  1. AccessDenied.aspx
  2. Confirmation.aspx
  3. Error.aspx
  4. Login.aspx
  5. ReqAcc.aspx
  6. SignOut.aspx
  7. WebDeleted.aspx


For replace the default list of pages as mentioned in above. We have a class SPWebApplication Under namespace Microsoft.SharePoint.Administration. SPWebApplication class have a SPCustomPage enum which has a following values.

public enum SPCustomPage
        {
            // Summary:
            //     Specifies that no page will be replaced with a custom page.
            None = 0,
            //
            // Summary:
            //     Specifies that the AccessDenied.aspx page will be replaced with a custom
            //     page.
            AccessDenied = 1,
            //
            // Summary:
            //     Specifies that the Confirmation.aspx page will be replaced with a custom
            //     page.
            Confirmation = 2,
            //
            // Summary:
            //     Specifies that the Error.aspx page will be replaced with a custom page.
            Error = 3,
            //
            // Summary:
            //     Specifies that the Login.aspx page will be replaced with a custom page.
            Login = 4,
            //
            // Summary:
            //     Specifies that the ReqAcc.aspx page will be replaced with a custom page.
            RequestAccess = 5,
            //
            // Summary:
            //     Specifies that the Signout.aspx page will be replaced with a custom page.
            Signout = 6,
            //
            // Summary:
            //     Specifies that the WebDeleted.aspx page will be replaced with a custom page.
            WebDeleted = 7,
        }

On Feature activation we can update the SharePoint2010 page with our own custom page. As mentioned in bellow.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            const string CustomAccessDeniedPage = "/_layouts/MyAccessDenied.aspx";
            const string CustomConfirmationPage = "/_layouts/MyConfirmation.aspx";
            const string CustomErrorPage = "/_layouts/MyAccessDenied.aspx";
            const string CustomLoginPage = "/_layouts/MyAccessDenied.aspx";
            const string CustomReqAccPage = "/_layouts/MyAccessDenied.aspx";
            const string CustomSignOutPage = "/_layouts/MyAccessDenied.aspx";
            const string CustomWebDeletedPage = "/_layouts/MyAccessDenied.aspx";

            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp != null)
            {
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied, CustomAccessDeniedPage))
                {
                    throw new ApplicationException("Cannot create the custom access denied mapping.");
                }

                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Confirmation, CustomConfirmationPage))
                {
                    throw new ApplicationException("Cannot create the custom Confirmation mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, CustomErrorPage))
                {
                    throw new ApplicationException("Cannot create the custom Error mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Login, CustomLoginPage))
                {
                    throw new ApplicationException("Cannot create the custom Login mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.RequestAccess, CustomReqAccPage))
                {
                    throw new ApplicationException("Cannot create the custom RequestAccess mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Signout, CustomSignOutPage))
                {
                    throw new ApplicationException("Cannot create the custom Signout mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.WebDeleted, CustomWebDeletedPage))
                {
                    throw new ApplicationException("Cannot create the custom  WebDeleted mapping.");
                }
                webApp.Update(true);

            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            const string AccessDeniedPage = "/_layouts/AccessDenied.aspx";
            const string ConfirmationPage = "/_layouts/Confirmation.aspx";
            const string ErrorPage = "/_layouts/Error.aspx";
            const string LoginPage = "/_layouts/Login.aspx";
            const string ReqAccPage = "/_layouts/ReqAcc.aspx";
            const string SignOutPage = "/_layouts/SignOut.aspx";
            const string WebDeletedPage = "/_layouts/WebDeleted.aspx";

            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp != null)
            {
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied,
                    AccessDeniedPage))
                {
                    throw new ApplicationException("Cannot create the access denied mapping.");
                }

                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Confirmation, ConfirmationPage))
                {
                    throw new ApplicationException("Cannot create the access denied mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, ErrorPage))
                {
                    throw new ApplicationException("Cannot create the access denied mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Login, LoginPage))
                {
                    throw new ApplicationException("Cannot create the access denied mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.RequestAccess, ReqAccPage))
                {
                    throw new ApplicationException("Cannot create the access denied mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Signout, SignOutPage))
                {
                    throw new ApplicationException("Cannot create the access denied mapping.");
                }
                if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.WebDeleted, WebDeletedPage))
                {
                    throw new ApplicationException("Cannot create the access denied mapping.");
                } 

                webApp.Update(true);

            }
        }




Comments

  1. Hello !
    It is very intresting post. Can you show me your
    MyAccessDenied.aspx ? I want to display on MyAccessDenied.aspx information about owners of site and i have error - > cant display page
    regards

    ReplyDelete
  2. Hi,
    You just need to copy the Access Dined page from the 14 hive location "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\AccessDenied.aspx" and modify the page text as well as the name of the page and then apply the above code in my case "MyAccessDenied.aspx".

    ReplyDelete
  3. Is it Possible to set it for single site collection. Instead of entire web application?

    ReplyDelete
    Replies
    1. It is not possible to set this for a single site collection.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

Send Email using SharePoint Rest API

Query suggestions in SP 2013 Using Rest API (/_api/search/suggest)