Friday, 11 April 2014

Using SPWebConfigModification

The SPWebConfigModification class is used to as the name suggests: to modify the web config; basically to my understanding what it does is, stores a collection of web config modifications in the content database and then when the ApplyWebConfigModifications method is called the changes are propagated to all the web configs on a front-end web server. This is the preferred manner to update your web config.

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
    };

}

and now we can call our module however many times we need from the feature activated event

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

}

but remember in SharePoint whatever we do we must also undo or pay the price of at least a week of our time.

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

}

now that we've made our web config modifications we have one more step to do, we have to actually deploy our Http Handler into the GAC, to do this open up your package manifest.


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