There are several reasons that cause the requirement to have your own Custom Access Denied Page in SharePoint 2013, like dynamical the text of the message or adding a a lot of dynamic page / form to gather information from the user or perform another operate. Once you build your page and acquire it deployed to Layouts Folder and use below Powershell Commands
1 2 |
$web = Get-SPWebApplication http://WebApplicationURL/ Set-SPCustomLayoutsPage -Identity AccessDenied –RelativePath "/_layouts/15/Folder/CustomAccessDeniedPage.html" -WebApplication $web |
Or
1 2 3 |
$web = Get-SPWebApplication http://WebApplicationURL/ $web.UpdateMappedPage([Microsoft.SharePoint.Administration.SPWebApplication+SPCustomPage]::AccessDenied, "/_layouts/15/Folder/CustomAccessDeniedPage.html ") $web.Update() |
Now, Let’s Check the set property with PowerShell command code:
1 2 |
$web = Get-SPWebApplication http://WebApplicationURL/ Get-SPCustomLayoutsPage -Identity AccessDenied -WebApplication $web | Format-List |
You will observe the updated worth however after you strive browsing you may get the default AccessDenied Page however not the custom access denied page. this is often a notable bug in SharePoint 2013 product. until currently with the current release of CU1 in Feb 2014, this issue isn’t been fixed.
Inorder to accomplish this, you’ll go together with HttpHandler or HttpModule. Here we tend to most popular to go with HttpModule and below is the code snippet for it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class CustomAccessDenied : IHttpModule { public void Init(HttpApplication app) { app.PreRequestHandlerExecute += new EventHandler(app_PreRequestHandlerExecute); } void app_PreRequestHandlerExecute(object sender, EventArgs e) { HttpContext context = HttpContext.Current; string str = Path.GetFileName(new Uri(context.Request.Url.AbsoluteUri).LocalPath); if (str.ToLower().Equals("accessdenied.aspx")) { context.Response.Redirect("/_layouts/15/customaccessdenied.aspx",true); } } public void Dispose() { } } |
Note: The custom access denied page [aspx] should has to Inherit from “Microsoft.SharePoint.ApplicationPages.AccessDeniedPage” class.
e.g. <%@PageLanguage=”C#”Inherits=”Microsoft.SharePoint.ApplicationPages.AccessDeniedPage”MasterPageFile=”~/_layouts/simple.master”%>
Finally, write your C# code inline in PlaceHolderMain in the .aspx page. (This is the drawback).