Created
November 22, 2012 01:31
-
-
Save michaelbramwell/4128925 to your computer and use it in GitHub Desktop.
HTTP 404 Error WebConfig Event Receiver for Sharepoint 2010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Security.Permissions; | |
using Microsoft.SharePoint; | |
using Microsoft.SharePoint.Administration; | |
using Microsoft.SharePoint.Security; | |
namespace Client.Project.Infrastructure.EventReceiver | |
{ | |
/// <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("00000000-0000-0000-0000-000000000000")] | |
public class CustomErrors : SPFeatureReceiver | |
{ | |
#region properties | |
private const string WebModificationOwnerName = "Automatic Http Error Pages Deploy Owner"; | |
#endregion properties | |
#region overrides | |
public override void FeatureActivated(SPFeatureReceiverProperties properties) | |
{ | |
var webApp = properties.Feature.Parent as SPWebApplication; | |
RemoveAllModifications(webApp, WebModificationOwnerName); | |
AddEntryIntoWebConfig(webApp); | |
} | |
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) | |
{ | |
var webApp = properties.Feature.Parent as SPWebApplication; | |
RemoveAllModifications(webApp, WebModificationOwnerName); | |
} | |
// 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) | |
//{ | |
//} | |
#endregion overrides | |
#region private methods | |
private void AddEntryIntoWebConfig(SPWebApplication webApp) | |
{ | |
RemoveAllModifications(webApp, WebModificationOwnerName); | |
string name, value, xpath, xPthRemove, xPthStatusCode; | |
xpath = "configuration/system.webServer"; | |
xPthRemove = "configuration/system.webServer/httpErrors[1=1]"; | |
xPthStatusCode = "configuration/system.webServer/httpErrors[2=2]"; | |
#region 404 | |
//<HttpErrors errorMode="Custom" existingResponse="Auto" > | |
name = "httpErrors[@errorMode='Custom'][@existingResponse='Auto']"; | |
value = "<httpErrors errorMode='Custom' existingResponse='Auto'></httpErrors>"; | |
ModifyWebConfig(webApp, xpath, name, value); | |
webApp.WebService.Update(); | |
//<remove statusCode="404" subStatusCode="-1" /> | |
name = "remove[@statusCode='404'][@subStatusCode='-1']"; | |
value = "<remove statusCode='404' subStatusCode='-1' />"; | |
ModifyWebConfig(webApp, xPthRemove, name, value); | |
//<error statusCode="404" prefixLanguageFilePath="" path="/_layouts/project/customerrors/PageNotFound.aspx" responseMode="ExecuteURL" /> | |
name = @"error[@statusCode='404'][@prefixLanguageFilePath=''][@path='/_layouts/project/customerrors/PageNotFound.aspx'][@responseMode='ExecuteURL']"; | |
value = @"<error statusCode='404' prefixLanguageFilePath='' path='/_layouts/project/customerrors/PageNotFound.aspx' responseMode='ExecuteURL' />"; | |
ModifyWebConfig(webApp, xPthStatusCode, name, value); | |
#endregion 404 | |
try | |
{ | |
webApp.WebService.ApplyWebConfigModifications(); | |
} | |
catch (Exception ex) | |
{ | |
RemoveAllModifications(webApp, WebModificationOwnerName); | |
webApp.Update(); | |
webApp.WebService.ApplyWebConfigModifications(); | |
throw new Exception(string.Format("An error was encounted, web config mods added in this current spfeature install have been removed. Error Details: {0}", ex.Message)); | |
} | |
} | |
private void ModifyWebConfig(SPWebApplication webApp, string path, string nameModif, string valueModif) | |
{ | |
SPWebConfigModification modification = new SPWebConfigModification(nameModif, path); | |
modification.Value = valueModif; | |
modification.Sequence = 0; | |
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; | |
modification.Owner = WebModificationOwnerName; | |
webApp.WebConfigModifications.Add(modification); | |
webApp.Update(); | |
} | |
private void RemoveAllModifications(SPWebApplication webApp, string owner) | |
{ | |
List<SPWebConfigModification> modificationsToRemove = webApp.WebConfigModifications.Where(m => m.Owner == owner).ToList(); | |
foreach (SPWebConfigModification modification in modificationsToRemove) | |
webApp.WebConfigModifications.Remove(modification); | |
webApp.Update(); | |
webApp.WebService.ApplyWebConfigModifications(); | |
} | |
#endregion private methods | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment