rIf you have ever put together a WCM/Publishign site in MOSS, chances are someone at some point has asked you the question “Can we put in a custom page instead of the default file not found one?”. In ASP.NET this is easy enough to do, you just update the CustomErrors section of your web.config file and away you go, but in SharePoint you’ll find it’s done slightly differently.
Basically if you have a look at the SPWebApplication object, you’ll see it has a property called “FileNotFoundPage”, which by default will be set to “sps404.html”. If you do a quick search over the 12 hive for this one you’ll see that is lives at %12%\template\layouts\1033. Open it up and you can see the page is driven from JavaScript and will redirect you to spsredirect.aspx to attempt to redirect you (which as far as I know will land you on the standard SharePoint error page with a file not found error as opposed to just the usual file not found error in your browser).
Lucky for us we can easily update the page that you are sent to when a file isn’t found, and you can even do it through a feature so you don’t have to go messing around in the 12 hive (which is evil). Firstly, set yourself up new feature, scope it to the web application and add a receiver to it. In the receiver add some code to update that FileNotFoundPage property to a custom file name that you create and update the web app.
public override void FeatureActivated(SPFeatureReceiverProperties Properties)
{
SPWebApplication webApp = Properties.Feature.Parent as SPWebApplication;
webApp.FileNotFoundPage = "custom404.html";
webApp.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties Properties)
{
SPWebApplication webApp = Properties.Feature.Parent as SPWebApplication;
webApp.FileNotFoundPage = "sps404.html";
webApp.Update();
}
I include the deactivating so that when you turn your feature off, the normal 404 handling is put back in place.
Next, make sure your WSP file with the feature in it also creates that custom file not found page for you – just take a copy of sps404.html and include it in your project. Lastly, update this file so that it will send your users to an appropriate page, there is one line of code in that html file to change for this:
STSNavigate("/_layouts/MyCustomPage.aspx?oldUrl=" + requestedUrl);
That page you put into this line and be anything, so you can put an ASPX page in the layouts folder that has some code behind it to handle the 404’s appropriately (maybe look up against a list of some sort and direct users to content that has been moved perhaps?). You can do whatever you need to here, the original URL that was requested comes to you in the query string there so you are free to use it as you wish.
One thing to note about this though is that it relies on JavaScript to work, if you hit a page that doesn’t exist when this page is called you will see that it takes you to a page with this text
“File Not Found. Please turn on client scripting or browse with a browser that supports client scripting to allow the 404 url dreict feature to work.”
(Note the typo in direct there – it actually spits it out like that, thats not one of my typos!)
This is done through the use of a meta refresh tag in that html file, so you could look at changing the value of that as well so that when users without JavaScript hit the page they are taken somewhere that displays a friendlier error screen as well, but it is something to keep in mind.
Other than that, this is all pretty straightforward and doesn’t take long to implement, so think about using it on your next site to add a little bit of polish to it :-)