Thursday, January 10, 2008

Finding and Removing a Rogue SharePoint Web Config Modification

During development of a small tool to enable binding redirection of .net libraries referenced in a SharePoint site, I found that ApplyWebConfigModifications() calls to the SPWebService were failing due to an existing SPWebConfigModification. The existing modification was atempting to access a path that does not exist in the web.config file.

To find the modification that is failing, I added a breakpoint and whilst debugging opened the following statement in QuickWatch in Visual Studio:

SPFarm.Local.Services.GetValue<SPWebservice>()

Then I navigated through the following properties in the Quick Watch window:
WebApplications/base/base/Non-Public members/BackingList

This offers access to all the web applications on the farm. For each web application under that node, open the WebConfigModifications node and check each one for the path that is reported to be failing. Then you can delete that faulty WebCofigModification using the following code:


        foreach (SPWebApplication thisApp in SPFarm.Local.Services.GetValue<SPWebService>().WebApplications)
        {
            for (int thisApp.WebConfigModifications.Counti > 0i--)
            {
                SPWebConfigModification mod 
thisApp.WebConfigModifications[i - 1];
                if 
(mod.Path.ToLower().Contains("[TEXT IN BAD PATH]"))
                      thisApp.WebConfigModifications.Remove(mod)
;
            
}
            thisApp.Update()
;
        
}


By deleting the faulty modification from the collection, the ApplyWebConfigModifications() call will now run to completion without raising an exception.

1 comment:

Peter said...

Thanks for this post. You set me on the right track for solving a config problem I am having. I came across this feature that I thought you would find useful. It enables you to edit the config list through Central Administration.

http://blog.thekid.me.uk/archive/2007/03/24/web-config-modification-manager-for-sharepoint.aspx