Wednesday, May 30, 2012

UpdateMappedPage in SharePoint 2010

 

Whenever there is error we get SharePoint 2010 default Error.aspx page. This page is provided by SharePoint without any CSS applied with very simple format. Most of the times we want our custom page to be displayed and in SharePoint 2010 this is possible using the UpdateMappedPage function of Web Application object.

Also this function is also capable to change the other custom mapping pages like

  • AccessDenied
  • Confirmation
  • Error
  • Login
  • RequestAccess
  • Signout
  • WebDeleted

This is part of Microsoft.SharePoint.Administration namespace.

The mapping of each page can be changed at any level at point either using PowerShell or using code behind.

I usually prefer to do this in feature activation event having scope “WebApplication”.

Following is code snippet which allows you to do so:

public class MyCustomPagesEventReceiver : SPFeatureReceiver
{
    const string customError = "/_layouts/MyApplication/MyPages/MyError.aspx";
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
        if (webApp != null)
        {
            if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.Error, customError))
            {
                 throw new ApplicationException("Error page could not be mapped, Contact System Administrator");
            }
            webApp.Update(true);
        }
    }
}

To Remove Mapping:

public class MyCustomPagesEventReceiver : SPFeatureReceiver
{
    public override void FeatureDeActivated(SPFeatureReceiverProperties properties)
    {
        SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
        if (webApp != null)
        {
             if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied, null))
             {
                   throw new ApplicationException("Cannot create the new access denied page mapping.");
             }
             webApp.Update(true);
        }
     }
}

No comments: