Pages

Search This Blog

Tuesday, January 10, 2012

Sharepoint Adding a Mapped URL programmatically

In Sharepoint 2010, you can retrieve the URL of some common Application Pages as AccessDenied.aspx, Error.aspx, Login.aspx, Signout.aspx etc. This has been done for the purpose of enabling the user to assign custom Application page instead of the default one. One way to do this is to add a Mapping attribute in the web.config file. But in Sharepoint 2010, there is a simpler way to do this.
We can make use of GetMappedPage and UpdateMappedPage  methods. This can be done by creating a Web scoped Feature and write your code in Feature Activated and Deactivated as:



public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
                try
                {
                    if (webApp != null)
                    {
                        if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied,
                            "/_layouts/YourCustomAppPagesFolder/CustomAccessDenied.aspx"))
                        {
                            throw new ApplicationException("Error adding mapped page.");
                        }

                        // File Not Found page is set differently.
                        webApp.FileNotFoundPage = "/_layouts/1033/YourCustomFolder/YourFileNorFoundPage.htm";
                        webApp.Update(true);
                    }
                }
                catch (Exception Ex)
                {

                }
            }


            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
                try
                {
                if (webApp != null)
                {
                    //you have to set the property back to null for setting it to default
                    webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied, null);

                    //File not found is handled differently
                    webApp.FileNotFoundPage = null;
                }
                }
                catch (Exception Ex)
                {
                }
            }
        }

No comments:

Post a Comment