Search This Blog

Sunday, February 26, 2012

Redirect to a new page, when we get 404.htm error page in the application



Create a custom Page Not Found is to create a site level feature receiver that will attach it to the webApplication instance.
On the activated event you would attach it like this:
    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
      if (properties != null)
      {
        try
        {
          SPSite site = properties.Feature.Parent as SPSite;
          System.Uri webApplicationUri = new Uri(site.Url);
          SPWebApplication webApplication = SPWebApplication.Lookup(webApplicationUri);
          if (webApplication != null)
            webApplication.FileNotFoundPage = "CustomError.html";
        }
        catch (Exception ex)
        {
         throw;
        }
      }
   }
And in your deactivated event you could unattach it by setting the same webApplication.FileNotFoundPage to null.
You will also have to save your Custom Page Not found page in the Layout.

No comments:

Post a Comment