SPWebConfigModifications Duplicate entries, applying to all Web Applications and not removing entries

Hi,

Hopefully someone can offer some advice, I may be missing something obvious here.

I'm currently trying to write a feature event receiver to apply web.config modifications that are required by a recent project. 

The feature is web application scoped & it doesn't activate by default on deployment, currently testing with a very small addition isn't working at all, I've followed numerous posts about duplicate entries & items being removed, none of which have helped.

Here is my code;

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
          
                SPWebConfigModification Modification = new SPWebConfigModification();
                Modification.Owner = "ConfigUpdater";
                Modification.Path = "configuration/appSettings";
                Modification.Name = "add[@key='Test']";
                Modification.Sequence = 0;
                Modification.Value = "<add key='Test' value='Server=TestServer;Database=TestDB;Trusted_Connection=True;' />";
                Modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                         
                if (!webApp.WebConfigModifications.Contains(Modification))
                {
                    webApp.WebConfigModifications.Add(Modification);
                    webApp.Update();
                    webApp.WebService.ApplyWebConfigModifications();
                }  
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            Collection<SPWebConfigModification> modsCollection = webApp.WebConfigModifications;
            int count = modsCollection.Count;

            for (int i = count - 1; i >= 0; i--)
            {
                SPWebConfigModification mod = modsCollection[i];
                if (mod.Owner == "ConfigUpdater")
                {
                    modsCollection.Remove(mod);                   
                }               
            }           
            webApp.Update();
            webApp.WebService.ApplyWebConfigModifications();          
        }

Upon activation on 1 web app, the entry gets added twice to the web.config of every web application in the farm & upon deactivation the entries remain in the web.config files.

On activation, after this line, webApp.WebConfigModifications.Add(Modification); the webApp is updated to include the entry & only once, however 2 entries for it appear in the web.config files & as mentioned for every web application.

On deactivation, after updating the web app, the modification is removed, however it remains in the web.config files.

I'm wondering if the code actually works, as I can't see anything wrong & if there is some caching occurring somewhere which is applying the extra line/s upon calling ApplyWebConfigModifications.

Any help on this would be much appreciated.

Thanks,

James

March 20th, 2015 10:49am

Hi James,

Per my understanding, there is an entry gets added twice to the web.config file of every web application in the FeatureActivated event.

As a common practice, I suggest you debug your code to see how it works during the process, it will provide more information for a better troubleshooting.

Also, you can test the code in a Console Application to see if it will work as expected.

Thanks         

Patrick Liang

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 1:41am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics