Friday, 4 July 2014

SharePoint 2010 Set Custom 404 Page using Feature

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
[caption id="attachment_9276" align="alignnone" width="300"]customerror customerror[/caption]
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

Set Custom404 page as Error Page.
Run the following PowerShell command from the SharePoint 2010 Management Shell:
(Get-SPWebApplication http://sp2010).FileNotFoundPage = "Custom404.html"
or
$webapp =Get-SPWebApplication http://<serverUrl>:<port>
$webapp.FileNotFoundPage = "Custom404.html"
$webapp.update()
5- Verify that the property is set by running the following command:
(Get-SPWebApplication http://<serverUrl>:<port>).FileNotFoundPage
or
$webapp =(Get-SPWebApplication http://sp2010).FileNotFoundPage

Steps:
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();
      }
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
if (properties != null)
{
SPSite currentSite = (SPSite)properties.Feature.Parent;
SPWebApplication webApplication = currentSite.WebApplication;
webApplication.FileNotFoundPage = "sps404.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.
Validate our code
Open SharePoint 2010 Management Shell
Run below commands
$webapp =(Get-SPWebApplication http://sp2010).FileNotFoundPage
$webapp
Here you will get output : custom404.html
Disable your feature...
After disabling the feature you can run same commands and check the out put; it should be "sps404.html"
$webapp =(Get-SPWebApplication http://sp2010).FileNotFoundPage
$webapp
Here you will get output : sps404.html
Final Code of the Feature
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
namespace PageNotFound.Features.Feature1
{
/// <summary>
/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should not be modified.
/// </remarks>
[Guid("fa7681d1-ea2b-41f9-a157-4ddd4e8571ec")]
public class Feature1EventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
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();
}
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
if (properties != null)
{
SPSite currentSite = (SPSite)properties.Feature.Parent;
SPWebApplication webApplication = currentSite.WebApplication;
webApplication.FileNotFoundPage = "sps404.html";
webApplication.Update();
}
}
// Uncomment the method below to handle the event raised after a feature has been installed.
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised before a feature is uninstalled.
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised when a feature is upgrading.
//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
//{
//}
}
}