I have a little issue here regarding the modification of Web.Config files in Feature Stamping (SP2010 [Web Application Level Feature, Activate on default])
I’m facing two strange Issues
- The applied modification like (adding a child node) appears multiple times in web.config.
- At feature deactivating, I’m removing the modification against the owner, it gets the modification, but
These are not removed.
I’m using the follow code snip during Feature Activation.
ModificationEntry[] enries =
{
new ModificationEntry("someName", "someSection", SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode)
};
SPWebApplication WebApp = (SPWebApplication)properties.Feature.Parent;
WebApp.WebConfigModifications.Clear();
foreach (ModificationEntry entry in enries)
{
// CreateModification simply return me SPWebConfigModification
SPWebConfigModification configModificationItem = CreateModification(entry, properties.Feature.DefinitionId.ToString());
if (!WebApp.WebConfigModifications.Contains(configModificationItem))
{
WebApp.WebConfigModifications.Add(configModificationItem);
}
}
WebApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
WebApp.Update();
This is what I’m doing at feature deactivation.
if (webApp != null)
{
Collection<SPWebConfigModification> collection = webApp.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 == properties.Feature.DefinitionId.ToString())
collection.Remove(configMod);
}
// Apply changes only if any items were removed.
if (iStartCount > collection.Count)
{
webApp.Update();
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
}
Please Comment !
1 Answer