none
Custom Access denied Page RRS feed

  • Question

  • Hi,

    I am trying to create a custom Access denied page. I am using Application page in which I am trying to show the Owner name, in case the user does not have access to the site/list/document library.

    I have tried the below code, but it does not work:

    <%@ Assembly Name="Microsoft.SharePoint.ApplicationPages,
    Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%> 
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomAccessDeniedPage.aspx.cs"
    Inherits="Microsoft.SharePoint.ApplicationPages.AccessDeniedPage" MasterPageFile="~/_layouts/simple.master" %> 
    <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> 
    <%@ Register
    Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral,
    PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register Tagprefix="Utilities"
    Namespace="Microsoft.SharePoint.Utilities"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral,
    PublicKeyToken=71e9bce111e9429c" %> 
    <%@ Import
    Namespace="Microsoft.SharePoint" %> 
    <%@ Assembly
    Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral,
    PublicKeyToken=71e9bce111e9429c" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitle"
    runat="server">
    <SharePoint:EncodedLiteral ID="EncodedLiteral5" runat="server"
    Text="Custom Access Denied" EncodeMethod='HtmlEncode' />
    </asp:Content>
    <asp:Content ID="Content2"
    ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
    <asp:Label ID="LabelUserName" runat="server" Visible="true"></asp:Label>  
    </asp:Content>
    <
    <asp:Content ID="Content4"
    ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <meta name="Robots" content="NOINDEX " />
    </asp:Content>
    
    
    //CS code
    
    using System;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using System.Net;
    using Microsoft.SharePoint.Utilities;
    using System.Web;
    using System.Diagnostics;
    using System.Web.UI.WebControls;
    using System.Text;
    using System.Linq;
    
    namespace Microsoft.SharePoint.ApplicationPages
    {
        public partial class CustomErrorPage : Microsoft.SharePoint.ApplicationPages.AccessDeniedPage
        {
            protected void Page_Load(object sender, EventArgs e)
            {          
               
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate
                    {
                        LabelUserName.Text = "Test Custom Page";                   
                       
                    });
                   
                }
                catch (Exception ex)
                {
                    
                }
            }
        }
    }
    

    I have currently put a sample label to check if the page load is hit and the label text shows. However, the label text is not getting changed.

    How to create a custom access denied page , showing the owner name(s), who can provide the access?

    Thanks

    Wednesday, February 22, 2017 7:10 AM

All replies

  • Hi,

    Thanks for the reply.

    I do not want to make any changes to OOB files.

    Thanks

    Wednesday, February 22, 2017 12:17 PM
  • Hi Venkatzeus,

    Can you try changing the name of your code behind partial class from CustomErrorPage to CustomAccesDeniedPage. Because in your aspx page CodeBehind file is pointing to the CustomAccessDeniedPage.

    Also try to use the default master page url in your aspx page.

    Thanks

     


    K Mohit


    • Edited by KumarMohiT Wednesday, February 22, 2017 2:54 PM
    Wednesday, February 22, 2017 2:53 PM
  • Hi,

    Thanks for the reply. I corrected both those and now on clicking "sign in as a different user", the custom error page is shown. I want the page to be shown only when there is a access issue. Below is the updated code:

    .aspx code
    
    <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
    <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
    <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APS.aspx.cs" Inherits="mycustomerrorpage.Layouts.CustomAccessDeniedPage.APS" DynamicMasterPageFile="~masterurl/default.master" %>
    
    <asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    
    </asp:Content>
    
    <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
          <asp:Label id="labelUsers" Text="Hi" runat="server" />
    </asp:Content>
    
    <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Application Page
    </asp:Content>
    
    <asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
    My Application Page
    </asp:Content>
    
    
    //aspx.cs
    using System;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    
    namespace mycustomerrorpage.Layouts.CustomAccessDeniedPage
    {
        public partial class APS : LayoutsPageBase
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                labelUsers.Text = "Test";
            }
        }
    }
    

    How to fix this?

    Thanks

    Thursday, February 23, 2017 9:50 AM