1

I am having an issue with SPWEBConfigModification. Instead of removing the item I added , it is adding a second copy of the added Line ! I am using SP2013 SP1 Aug 2016 CU. (I have changed the value of "Owner" to protect the innocent) I have consulted the following articles :- https://blog.crsw.com/2007/09/20/how-to-modify-the-web-config-file-in-sharepoint-using-spwebconfigmodification/ , https://spmatt.wordpress.com/2012/10/10/jumping-the-hurdles-of-using-spwebconfigmodification-to-update-the-web-config/ , web.config Entries not Being Removed During Feature Deactivation , Web.Config Modification not removing

                Collection<SPWebConfigModification> modsCollection = webApplication.WebConfigModifications;
            Logger.Log(logSource, "Modification Count :- " + modsCollection.Count.ToString());
            // Find the most recent modification of a specified owner
            int modsCount1 = modsCollection.Count;
            List<SPWebConfigModification> toRemove = new List<SPWebConfigModification>();
            for (int i = modsCount1 - 1; i > -1; i--)
            {

                if (modsCollection[i].Owner == "MyName")
                {
                    Logger.Log(logSource, "Found It :- " + modsCollection[i].Owner);
                    Logger.Log(logSource, "Modification = " + modsCollection[i]);
                    Logger.Log(logSource, "Value = " + modsCollection[i].Value);
                    toRemove.Add( modsCollection[i]);
                }
            }

            // Remove it and save the change to the configuration database 
            foreach (SPWebConfigModification mod in toRemove)
            {
                modsCollection.Remove(mod);
                webApplication.Update();
            }


            Logger.Log(logSource, "Modification Count after removing Found Config :- " + modsCollection.Count.ToString());



            // Reapply all the configuration modifications
            webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();



        }

THis is the modification itself :-

                SPWebConfigModification myModification = new SPWebConfigModification();
            myModification.Path = "configuration/system.webServer/modules";
            myModification.Name = "add[@type='NLC.SP.ShareManagerHttpModule.SPShareManagerModule, NLC.SP.ShareManagerHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e74396ad484caf03'][@Name='SPShareManagerModule']";
            myModification.Sequence = 0;
            myModification.Owner = "MyName";
            myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            myModification.Value = "<add type='NLC.SP.ShareManagerHttpModule.SPShareManagerModule, NLC.SP.ShareManagerHttpModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e74396ad484caf03' name='SPShareManagerModule' />";
            webApplication.WebConfigModifications.Add(myModification);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.