1

I am using the following code to remove a web.config modification which added to web.config via one feature

        Collection<SPWebConfigModification> applicationModifications = webApplication.WebConfigModifications;
        SPWebConfigModification browserCapsConfig = null;

        int modsCount = applicationModifications.Count;
        for (int i = modsCount - 1; i > -1; i--)
        {
            if (applicationModifications[i].Owner == "OWNER_NAME")
            {
                browserCapsConfig = applicationModifications[i];
            }
        }

        if (browserCapsConfig != null)
        {
            applicationModifications.Remove(browserCapsConfig);
            webApplication.Update();
            webApplication.WebService.ApplyWebConfigModifications();
        }

All web.config modifications are retrieved in collection properly but the thing is while removing it, it is removed from the collection (unable to retrieve it anymore) but not from the web.config. The added modification still there. I tried the same code in SharePoint feature and a console application and getting the same behavior.

Anything missing I should do to remove it from the web.config file?

1 Answer 1

3

There's nothing wrong with your code above, so this almost certainly has something to do with your SPWebConfigModification object's "Name" value.

The SPWebConfigModification.Name property (and the Name parameter in the SPWebConfigModification's constructor) are actually XPath expressions that uniquely identify the config entry. SharePoint uses this XPath expression to locate the modification when you try to remove or update it, hence if you haven't set it properly then it can't be removed.

See these resources for more info and correct naming conventions:

http://blogs.devhorizon.com/reza/2008/01/05/spwebconfigmodifications-top-6-issues/

http://social.msdn.microsoft.com/Forums/da-DK/sharepointdevelopment/thread/03f5b878-0f74-48ab-8049-ed4264a0b62a

4
  • I changed the Name to be like this configModification.Name = "add[@name='NAME']"; but I'm still getting the same behavior! Any suggestions?
    – Ahmed Atia
    Feb 29, 2012 at 10:21
  • Something to always keep in mind with these programmatic config changes is that SharePoint will only ever remove what it has placed there itself. I.e. if you put in a config entry by hand (or update it) then the actual file is no longer in sync with the config modifications in the content db and you'll never remove it. Download the free "SharePoint Manager 2010 V2" tool, this allows you to drill down and see exactly what config changes are in the content database, plus their proper names etc. that'll tell you straight away if what mods SharePoint "thinks" are in your config file.
    – user4545
    Feb 29, 2012 at 14:17
  • In the same vein, you should try blowing away your site and re-inserting all your config changes from scratch. As soon as things get "out of sync" it's generally a lost cause.
    – user4545
    Feb 29, 2012 at 14:26
  • Worked for me... first I used xpath to target the nodes I wanted to remove and then, when that wasn't enough, I 'reset' my web.config by manually deleting the nodes so my code could add fresh new nodes.
    – Aaronster
    Oct 29, 2013 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.