Tuesday, September 13, 2011

Use of UpdateMappedPage in sharepoint 2010

Hi Friend,

In sharepoint 2007, we were not able to open our custom site page instead of default page. Suppose you have requirement like that you need to open your custom page instead of error.aspx / accessdenied.aspx / login.aspx / signout.aspx etc.
Now we can override these page using UpdateMappedPage method in sharepoint 2010. Here you can see how we can update all the pages.

I am simply create a event receiver to update the error.aspx, Accessdenied.aspx and Signout.aspx pages. First you need to create three .aspx pages respectively and create new folder in layout folder of root directory. Then put all the pages in newly created folder. (In my case that is errorpage.aspx, accessdenied.aspx and logout.aspx)
Now go back to your code and use below code:

  const string CustomErrorPage = "/_layouts/Customerorrpages/errorpage.aspx";
  const string CustomAccessdeniedPage = "/_layouts/Customerorrpages/accessdenied.aspx";
  const string CustomLogoutPage = "/_layouts/Customerorrpages/logout.aspx";

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    SPSite web = properties.Feature.Parent as SPSite;
                    SPWebApplication webApp = web.WebApplication;
                    using (SPSite site = webApp.Sites[0])
                    {
                        using (SPWeb rootWeb = site.RootWeb)
                        {
                            if (webApp != null)
                            {
                               
                                webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, CustomErrorPage);
                                webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied, CustomAccessdeniedPage);
                                webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Signout, CustomLogoutPage);
                                webApp.Update(true);
                            }
                        }
                    }
                }
                catch(Exception ex)
                {
                }
            });
         }
           
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                   
                    SPSite web = properties.Feature.Parent as SPSite;
                    SPWebApplication webApp = web.WebApplication;
                   
                    using (SPSite site = webApp.Sites[0])
                    {
                        using (SPWeb rootWeb = site.RootWeb)
                        {
                            if (webApp != null)
                            {
                              
                         webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, null);
                         webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied, null);
                         webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Signout, null);
                                webApp.Update(true);
                            }
                        }
                    }
                }
                catch(Exception ex)
                {
                   
                }
            });
       }
Now you can see, if you activate this feature then it will override the pages and once you deactivate this then again value set is null.

Please provide your feedback

2 comments:

  1. It doesn't work. When I sign in as different user with the user that doesn't have permission then it redirects to the default access denied page. When I check through the console application my web application mapped page is already changed. But it doesn't reflect any changes.

    ReplyDelete