Ram Prasad Meenavalli

Creating custom Page Not Found Page

Published on 9-March-2011 | Comments

If we request for a broken URL or an invalid URL on a SharePoint site, we get the return status as 404 from the server and we get the default 404 page. But, for many sites (on internet/intranet) we need a custom page with a custom UI whenver the return status is 404. Follow the below steps to create a custom 404 page for a SharePoint WebApplication.

Create a custom 404 page

Before creating a new custom 404 page, lets understand how the defaut sps404.html works. There is a html page named 'sps404.html' at '\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\LangID' This is a simple html page which is opened when the status is 404 for a requested URL. If we go through the code of this HTML file, we can understand that it redirects to a page at /_layouts/spsredirect.aspx. Now follow the below steps and create a custom 404 page.

  • Make a copy of the default sps404.html and name it as custom404.html
  • Change the redirection URL (in the copied file, ie., custom404.html) to any other URL where your custom page not found page is created. (this page can be in the pages library or an application page in _layouts

Note:If we need a simple static page, then we can put that content in the custom404.html file itself.

  • Now copy the custom html file to the below path

\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\LangID. (1033 is the language ID for U.S. English)
So, we have the custom 404 page ready.

Setting the Page Not Found reference for the Web Application

Now the reference for Page not Found for the web application should be changed through API. SPWebApplication object has a property called FileNotFoundPage. This property should be set with the name of the newly created page. As a best practice we will write a feature which sets the Custom Page Not Found Page for a web application on activation and resets the page not found on deactivating.
The Feature Activated code should be as shown below

public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; if (webApp != null) { webApp.FileNotFoundPage = "custom404.html"; webApp.Update(true); } }

The Feature Deactivating method should have the code as shown below

public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; if (webApp != null) { webApp.FileNotFoundPage = ""; webApp.Update(true); } }

After activating this feature, the custom page not found is set for the web application. And on deactivating, it is reset.

If we don't want to write a feature for this task, the code which is there in the FeatureActivated method (with some changes) can be kept in a simple console application and can be executed. The code to be executed through a console application should be as shown below

Microsoft.SharePoint.Administration.SPWebApplication webapp = Microsoft.SharePoint.Administration.SPWebApplication.Lookup(new Uri("URL OF THE SITE")); webapp.FileNotFoundPage = "custom404.html"; webapp.Update();

This approach works for a SharePoint 2010 site also. But instead of setting the custom error page through SharePoint API, we can use powershell in SharePoint 2010. Read how to do this using powershell in the next article


View Ram Prasad Meenavalli's LinkedIn profile

SharePoint Technology Evangelist