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.
- 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) - 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.
- Create empty Sharepoint Project.
- Create your costom Access Denied Page in Layouts e.g
/_layouts/MyCustomLayout/AccessDenied.aspx - 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 pagepublic 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
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?
ReplyDeleteYou can inherit from the "AccessDeniedPage" in "Microsoft.SharePoint.ApplicationPages" assembly. This page handles this functionality.
ReplyDeleteThanks Anmol for the tip! This saved my day.
ReplyDeleteI 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?
This is working in SP 2010 but not in SP 2013. Any tips/tricks to make it working in SP 2013.
ReplyDelete