Create a custom HTTP404 rediect page in SharePoint 2010

By default, SharePoint takes you to its own 404 NOT FOUND page if you enter an Url which does not exist in the site.

My client has requested to display a 404 Not Found page with the custome master page and page layout style. After a few searches, some articles are found. Chaks’ and Ritu’s ones were particular helpful.

Default SharePoint 404 error pages are stored at this location:

14\TEMPLATE\LAYOUTS\1033\ERRORV4.html
14\TEMPLATE\LAYOUTS\1033\sps404.html

1. Create a error page on the site that has the publishing page layout and custom master page. I created a page called customError.aspx under the root site collection under the Pages folder.

2. Make a copy of sps404.html and name it custom404.html. Place it in the same location as sps404.html

3. Open custom404.html. Change the URL inside STSNavigate to your customError.aspx page url

customError

4. The next step is to set custom404.html as the 404 redirection page for your specific web application. we are accomplishing this by creating a custom404 feature and as the feature activates, it sets the FileNotFoundPage to the custom404.html. custom404.html then redirects the page to customError.aspx page

Open Visual Studio, create a new project, choose Empty SharePoint Project and change to .Net Framework 3.5. Name the project “PageNotFound”

Choose Deploy as a farm solution

Right click on Features, Add Feature, change the Scope to Site.

Right click on Feature1.feature, Add Event Receiver.

Open the EventReceiver.cs file, add this line above the namespace.

using Microsoft.SharePoint.Administration;

Paste the following code inside public class.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
      if (properties != null)
      {
        SPSite currentSite = (SPSite)properties.Feature.Parent;
        SPWebApplication webApplication = currentSite.WebApplication;
         webApplication.FileNotFoundPage = "custom404.html";
         webApplication.Update();
      }
}

Right click on the project and Deploy.

Now if you go to a incorrect page, you will be redirected to your custom error page.

Reference:
http://blog.mastykarz.nl/sharepoint-2010-page-not-found-404/
http://chakkaradeep.com/index.php/customizing-the-404-redirection-in-sharepoint/
http://ritutechworld.blogspot.co.nz/2010/06/custom-error-page-in-sharepoint-2010.html
http://microsoftechies.wordpress.com/2012/10/08/error-occurred-in-deployment-step-activate-features-unable-to-cast-object-of-type-microsoft-sharepoint-spweb-to-type-microsoft-sharepoint-spsite/

Advertisements

One thought on “Create a custom HTTP404 rediect page in SharePoint 2010

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s