Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

When a user has not been given permissions to a particular site they receive the ugly "Error: Access Denied" page.

Is it possible to capture this on one particular team site (via custom webpart) and redirect the user to a custom access denied page that displays a more friendlier message (ex: "Thanks for your interest in [my company's team site]. Access has been limited at this time to physicians, fellows, physician extenders, clinical pharmacists, and nurses who are involved in patient care. If you need access for direct patient care and you have received this message, please contact info@mycompany.com".

I found examples using SPSecurity.CatchAccessDeniedException, but it looks like user is being redirected to AccessDenied.aspx before the code gets executed. I placed the code in Page_PreRender()

share|improve this question
add comment

2 Answers 2

Courtesy of Yaroslav Pentsarsky - EndUserSharePoint.com

  1. First you can copy an existing page from the Template/Layouts, in our case we'll use AccessDenied.aspx. You can give it any name. If you’re using a Visual Studio solution simply add this new file to your solution structure underLayouts mapped folder.

  2. Make any desired changes to the newly created page. If you made a copy of an existing page, remember that it’s referencing the original assembly and you still need to follow the structure outlined in the page placeholders to avoid the page erroring out. Alternately, you can create your own page in VS2010 which can reference its own assembly.

  3. Assuming the page has been deployed to the layouts folder. Execute the following PowerShell to set it as a default page for the role, in our case AccessDenied page role.

    PS C:\Users\Administrator> $site = get-spsite "http://intranet.contoso.com"

    PS C:\Users\Administrator> $webApp = $site.WebApplication

    PS C:\Users\Administrator> $webapp.UpdateMappedPage(1, "/_layouts/AccessDeniedNew.aspx")

    True

    PS C:\Users\Administrator> $webapp.Update()

    PS C:\Users\Administrator>

In here, the value of “1″ in UpdateMappedPage, specifies a reference to an enumeration value for other page roles available, see SPCustomPage Microsoft reference for more info.

  • We’re set. Since we’re testing AccessDenied page, I’m going to log in as a reader and access “Site collection administrators” page by URL.s a result I am getting my customized AccessDenied page.

NOTE: The custom page must always be hosted in _layouts, otherwise the UpdateMappedPage will fail. If you need to show your content page as a result – you can execute a redirect from the custom page in _layouts to your content page.

share|improve this answer
    
If I create an Access Denied message targeting a specific site (with a specific email to contact for any questions) , can I make this work for just this one site? What I mean is...If a user doesnt have access to a different site, I dont want to alter that AccessDenied.aspx message. –  ninel Aug 16 '12 at 13:02
    
This is an all or nothing thing. It is an .aspx page so you could get fancy with the logic to differentiate. Another method is to utilize URL rewrite module of IIS and configure some redirect rules to a custom page. With this you could create a rule that only affects a single site by redirecting from /*site*/_layouts/accessdenied.aspx to another page the user does have access too. –  Jesus Shelby Aug 16 '12 at 18:50
    
One more question... I need this for users who do not have access to the main landing page of the site. I created an application page and added an Event Receiver to a feature, but it seems like this does not work. User is ALWAYS redirected to the generic AccessDenied.aspx. Am I doing something wrong? –  ninel Aug 17 '12 at 14:49
    
I've tried this, but the powershell script seems not to have any effect. It still loads the default accessdenied page. –  BGM Jan 12 '13 at 23:37
add comment

As an alternate you can use DoesUserHavePermissions property of spweb (SPSecuribleObject) and redirect to another page through code.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.