The SPWebConfigModification class has a number of properties that need to be set:
- Path: an xPath to where you want your modification to go
- Name: a unique name saved in the content database (i think) and used by the SPWebConfigModification class to ensure that multiple entries of the modification do not occur.
- Value: the actual web config modification
- Type: What kind of modification: SPWebConfigModification.SPWebConfigModificationType
- EnsureChildNode
- EnsureAttribute
- EnsureSection
- Owner: who owns the particular modification,
- Sequence: set the order in which the modifications occur, i'd start 10 and incriment in 10s.
now in the class definition there's an example of how to hand code all the values for a safe control, but what i want to do is add an httpHandler and I want to use reflection so that I can have one function that would handle adding any Handler to my web config.
using reflection we can create a function that just extracts all the details like so
public SPWebConfigModification GetModule(Type ModuleType)
{
Assembly ModuleAssembly = ModuleType.Assembly;
string NameSpace = ModuleAssembly.GetTypes().Select(t =>
t.Namespace).FirstOrDefault();
string ClassName = ModuleType.Name;
string AssemblyName = ModuleAssembly.FullName;
return new SPWebConfigModification
{
Path = "configuration/system.webServer/modules",
Name = String.Format("add[@name='{0}']", NameSpace),
Value = String.Format("<add
name='{0}' type='{0}.{1}, {2}' />",
NameSpace,ClassName,AssemblyName),
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Owner = String.Format("{0}.{1}", NameSpace, ClassName),
Sequence = 10
};
}
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var ErrorSwap_WebConfigMod = GetModule(typeof(ErrorSwapModule));
SPWebService ser = SPWebService.ContentService;
if (!ser.WebConfigModifications.Contains(ErrorSwap_WebConfigMod))
{
ser.WebConfigModifications.Add(ErrorSwap_WebConfigMod);
ser.Update();
ser.ApplyWebConfigModifications();
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebService ser = SPWebService.ContentService;
var ErrorSwap_WebConfigMod = GetModule(typeof(ErrorSwapModule));
if
(ser.WebConfigModifications.Contains(ErrorSwap_WebConfigMod))
{
ser.WebConfigModifications.Remove(ErrorSwap_WebConfigMod);
ser.Update();
ser.ApplyWebConfigModifications();
}
}
double click on your package and now hit ad assembly from project output
next from the source project drop down select your swap library, where your http module resides
after that make sure to add your namespace as a safe control
with your namespace filled in click on the location text box and this will enable the ok button, hit the ok button and deploy your project:
your deployment should
- put your http handler into the gac
- deploy your custom error pages to the pages library of your root site
- add the appropriate web config entry