Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm writing a web application scoped feature receiver to make changes to web.config using SPWebConfigModification. Adding entries is working well. My problem is removing the entries. When stepping through the debugger, I can see them being discovered by Owner name and removed from the WebConfigModifications collection. However, on inspection of the web.config file, these entries are still present. In my ULS I can see the FeatureReceiver running and completing for FeatureDeactivating. There are no other entries.

Does anyone have any ideas on what to do to?

public override void FeatureDeactivating(SPFeatureReceiverProperties properties){
    SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
    Collection<SPWebConfigModification> modsToRemove = new Collection<SPWebConfigModification>();

    foreach (SPWebConfigModification mod in webApp.WebConfigModifications)
    {
        if (mod.Owner == "MyUniqueName")
            modsToRemove.Add(mod);
    }

    if (modsToRemove.Count > 0)
    {
        int count = modsToRemove.Count;

        for (int i = 0; i < count; i++)
        {
            webApp.WebConfigModifications.Remove(modsToRemove[i]);
            webApp.Update();
        }
        webApp.WebService.ApplyWebConfigModifications();
    }
}
share|improve this question
    
Did you get this to work? I have been working with the same class and have noticed some weird behavior of this class when removing modifications. I can remove the configuration modifications, see that they're being removed, count the number removed, verify they are removed, but the entries themselves are not removed from the file in some cases. If there is more than one modification affecting the same node, I've noticed this occurring. –  RJ Cuthbertson Feb 28 '13 at 19:39
    
@rjcup - Please excuse my tardy reply. I've been on a month-long holiday. I can add web.config entries OK but cannot remove them from the web.config. Like you, I can see they're being removed (in the debugger) but the web.config file doesn't update. I'm not too fond of my workarounds either. –  Mike H Apr 2 '13 at 2:21
    
I posted a similar question to the OZMOSS mailing list and received a reply from Matt Menezes, pointing to a detailed post on his blog which includes a lot of detail and cross-links: spmatt.wordpress.com/2012/10/10/… –  Mike H Apr 9 '13 at 5:34

1 Answer 1

It appears that this is because you are calling ApplyWebConfigModifications() on the wrong SPWebService object.

From How To: Modify the web.config file in SharePoint using SPWebConfigModification:

Use the ApplyWebConfigModifications() method on the SPWebApplication object you are saving your SPWebConfigModification object to. I don’t understand why this works over the other way, but it solved the problem for me. Example:

myWebApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

Note: This code did not work across the farm (I could not tell you why).

SPFarm.Local.Services.GetValue< SPWebService>().ApplyWebConfigModifications();

Try this:

if (modsToRemove.Count > 0)
{
    int count = modsToRemove.Count;

    foreach (SPWebConfigModification modToRemove in modsToRemove)
    {
        webApp.WebConfigModifications.Remove(modToRemove);
        webApp.Update();
    }
    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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