Adding and Removing keys from appSettings in web.config
Today, I was struggling with adding and removing entries from web.config/appsettings in my feature receiver class. Well, it was quite easy to add , but removing was giving me a real hard time. I used my perfect indexing tool and I came across these great posts from Daniel Larson and Tony Bierman. By first look at their posts I realized that removing requires a *right* call to SPWebConfigModification constructor, otherwise it won’t ever happen. Both posts are for adding Ajax http handlers, but one can easily alter them to make the solution work for appsettings as well.
protected void ModifyWebApplication(SPWebApplication app, bool removeModification)
{SPWebConfigModification modification = new SPWebConfigModification(“add[@key=’TotalDigits’]”, “configuration/appSettings”);
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode ;
modification.Value = “<add key=\”TotalDigits\” value=\”21\” />”;
modification.Sequence = 0;
if (removeModification)
app.WebConfigModifications.Remove(modification);
else
app.WebConfigModifications.Add(modification);
SPFarm.Local.Services.GetValue().ApplyWebConfigModifications();}