/ .net

SharePoint web.config modifications

WSP deployment packages is the MSI for SharePoint. It really helps server administrators manage deployment as well as developers packing functionality and content in compact packages. With the introduction of Visual Studio 2010 it became easier to produce all kinds of WSPs right in your favorite dev env. Custom web.config modifications is one aspect Visual Studio doesn't handle though. And reading reading the documentation and blog posts on how to use the SPWebConfigModification class might make you think it's somewhat fuzzy. It isn't, but as most SharePoint API related it easily breaks if not used exactly the way the API developer thought it should be used. And if called directly you end up with lots of tedious code for adding simple configuration let alone your massive WCF configuration. So I wrote a small class that parses an app.config file and returns the modifications SharePoint needs to add/remove elements to the web configuration. Example:

SPWebService webApp = SPWebService.ContentService;
using (var ms = new MemoryStream(Resources.MyConfig))
    using (var reader = XmlReader.Create(ms))
        var mods = ConfigParser.GetAddMods(reader, Owner);
        // use GetRemoveMods() to remove it again
        foreach (var mod in mods)
                  .Add(mod); // or Remove()

To remove the configuration again simply replace the two method calls as specified in the comments. Download the ConfigParser class and have a look.