Search This Blog

Tuesday, 18 December 2012

Customizing AccessDenied page in SharePoint 2010 using PowerShell

Hi All,

In my previous post. I have given a simple tips to redirect Access Denied page, when user(s) does not have proper permission.

In this post we are going to see how to customize the page.

Its give better result when building a custom solution in SharePoint, we often need to balance out-of-the-box functionality along with the custom look and feel. Now here I am going to deal with AccessDenied.aspx system page, already we have seen how to redirect to AccessDenied page.

We can perform this without changing the original files. Here are the steps

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 under Layouts 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.

PowerShell Script:

PS C:\Users\Administrator> $site = get-spsite "http://Manikandan-pc:2020"
PS C:\Users\Administrator> $webApp = $site.WebApplication
PS C:\Users\Administrator> $webapp.UpdateMappedPage(1, "/_layouts/CustomAccessDeniedPage.aspx")
PS C:\Users\Administrator> $webapp.Update()
PS C:\Users\Administrator>

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

4. Done We’ve set. Since we’re testing AccessDenied page, I’m going to log in as a reader and access “Site collection administrators” page by URL.

As a result I am getting my customized AccessDenied page:

5. To reverse the customization, execute:

PS C:\Users\Administrator> $webapp.UpdateMappedPage(1, $null)
PS C:\Users\Administrator> $webapp.Update()
PS C:\Users\Administrator>

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.

Like this we can perform on following system page also,


Note: Reset IIS once, for immediate reflection.

No comments:

Post a Comment