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 have a problem with removing web.config entries. To add entries with the SPWebConfigurationModification is no problem, but whether i try to remove this entries again nothing happens. Any idea why?

Here is the code example i've used:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections;
using System.Collections.ObjectModel;

namespace Company.xxx.yyyy.zz.xxxx
{
    class MyChangeWebConfigEntries : SPFeatureReceiver
    {
        private static String OwnerId = "MyWebConfigModificationOwner";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                this.RemoveExistingWebConfigModifications();

                // Get an instance of my local web application 
                SPWebApplication webApp = SPContext.Current.Site.WebApplication; 

                webApp.WebConfigModifications.Clear(); 

                SPWebConfigModification mod = new SPWebConfigModification();
                mod.Path = "configuration/configSections";
                mod.Sequence = 0;
                mod.Name = "sectionGroup[@name='applicationSettings']";
                mod.Owner = MyChangeWebConfigEntries.OwnerId;
                mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                mod.Value = "<sectionGroup name='applicationSettings' type='System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'></sectionGroup>";

                // Add my new web.config modification. 
                webApp.WebConfigModifications.Add(mod);

                ////Add Section
                mod = new SPWebConfigModification();
                mod.Path = "configuration/configSections/sectionGroup[@name='applicationSettings']";
                mod.Name = "section[@name='Company.xxx.yyyy.zz.xxxx.EventHandler.Properties.Settings']";
                mod.Sequence = 0;
                mod.Owner = MyChangeWebConfigEntries.OwnerId;
                mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                mod.Value = "<section name='Company.xxx.yyyy.zz.xxxx.EventHandler.Properties.Settings' type='System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' requirePermission='false' />";
                webApp.WebConfigModifications.Add(mod);

                mod = new SPWebConfigModification();
                mod.Path = "configuration/configSections/sectionGroup[@name='applicationSettings']";
                mod.Name = "section[@name='Company.xxx.yyyy.zz.xxxx.Utilities.Properties.Settings']";
                mod.Sequence = 0;
                mod.Owner = MyChangeWebConfigEntries.OwnerId;
                mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                mod.Value = "<section name='Company.xxx.yyyy.zz.xxxx.Utilities.Properties.Settings' type='System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' requirePermission='false' />";
                webApp.WebConfigModifications.Add(mod);

                ////ApplicationSettings
                mod = new SPWebConfigModification();
                mod.Path = "configuration";
                mod.Name = "applicationSettings";
                mod.Sequence = 0;
                mod.Owner = MyChangeWebConfigEntries.OwnerId;
                mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                mod.Value = "<applicationSettings></applicationSettings>";
                webApp.WebConfigModifications.Add(mod);

                mod = new SPWebConfigModification();
                mod.Path = "configuration/applicationSettings";
                mod.Name = "Company.xxx.yyyy.zz.xxxx.EventHandler.Properties.Settings";
                mod.Sequence = 0;
                mod.Owner = MyChangeWebConfigEntries.OwnerId;
                mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                mod.Value = "<Company.xxx.yyyy.zz.xxxx.EventHandler.Properties.Settings></Company.xxx.yyyy.zz.xxxx.EventHandler.Properties.Settings>";
                webApp.WebConfigModifications.Add(mod);

                mod = new SPWebConfigModification();
                mod.Path = "configuration/applicationSettings/Company.xxx.yyyy.zz.xxxx.EventHandler.Properties.Settings";
                mod.Name = "setting [@name='XXX_EventHandler_XXXServiceProxy_RuleNumberService'] [@serializeAs='String']";
                mod.Sequence = 0;
                mod.Owner = MyChangeWebConfigEntries.OwnerId;
                mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                mod.Value = "<setting name='XXX_EventHandler_XXXServiceProxy_RuleNumberService' serializeAs='String'><value>http://localhost:81/xxxservice/xxxservice.asmx</value></setting>";
                webApp.WebConfigModifications.Add(mod);

                mod = new SPWebConfigModification();
                mod.Path = "configuration/applicationSettings/Company.xxx.yyyy.zz.xxxx.EventHandler.Properties.Settings";
                mod.Name = "setting [@name='XXX_TimerJob_UseMinuteSchedule'] [@serializeAs='String']";
                mod.Sequence = 0;
                mod.Owner = MyChangeWebConfigEntries.OwnerId;
                mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                mod.Value = "<setting name='XXX_TimerJob_UseMinuteSchedule' serializeAs='String'><value>False</value></setting>";
                webApp.WebConfigModifications.Add(mod);

                // Save web.config changes. 
                webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

                // Serialize the web application state and propagate changes across the farm. 
                webApp.Update(); 
            }
            catch (Exception ex)
            {

            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                this.RemoveExistingWebConfigModifications();
            }
            catch (Exception ex)
            { 

            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }


        private IList GetExistingWebConfigModifications()
        {
            SPWebService service = SPWebService.ContentService;
            ArrayList myWebConfigMods = new ArrayList();

            foreach (SPWebConfigModification mod in service.WebConfigModifications)
            {
                if (mod.Owner == MyChangeWebConfigEntries.OwnerId)
                {
                    myWebConfigMods.Add(mod);
                }
            }

            return myWebConfigMods;
        }

        private void RemoveExistingWebConfigModifications()
        {
            SPWebApplication myWebApp = SPContext.Current.Site.WebApplication;

           if (myWebApp != null)
           {
              Collection<SPWebConfigModification> collection = myWebApp.WebConfigModifications;

              int iStartCount = collection.Count;

              // Remove any modifications that were originally created by the owner.
              for (int c = iStartCount - 1; c >= 0; c--)
              {
                 SPWebConfigModification configMod = collection[c];

                 if (configMod.Owner == MyChangeWebConfigEntries.OwnerId)
                    collection.Remove(configMod);

              }

              // Apply changes only if any items were removed.
              if (iStartCount > collection.Count)
              {
                 myWebApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                 myWebApp.Update();
              }
            }
        }
    }
}
share|improve this question
1  
You might like to give a more concise code snippet next time that reproduces your problem. Thanks for your question and welcome to SharePoint Overflow! –  Alex Angas Oct 26 '09 at 13:02
    
Are there any errors in the ULS logs, or the Event viewer? If you run the RemoveExistingWebConfigModifications() method from a console application, does it work? Is the timer service running? –  Paul Schaeflein Oct 27 '09 at 17:09

3 Answers 3

Removing all of the entries is a common problem, especially if you try and do it as you have done (and to be honest how you would expect it to be done).

The trick is to remove all of the entries based on a specified Owner, you could use the .Net Class name for example.

Vince Rothwell has posted a great article on this and this is what I would regard as being a Best Practice. It also has the advantage that if you ever remove one of your modifications and redeploy the old entry is cleaned up correctly.

share|improve this answer

It's very crucial when to call the Update() and when to call the ApplyWebConfigModifications() methods.

Here is a code snippet where I remove a particular WebConfigModication from the List.

webApplication.WebConfigModifications.RemoveAt(modificationId);
webApplication.Update();
webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
share|improve this answer

The key to a correct modification are .Path and .Name attributes. If those are not set correct you can add smth in web.config but the removing wont work.

share|improve this answer

Your Answer

 
discard

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