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();
}
}
}
}
}