Home > MOSS 2007 > Adding and Removing keys from appSettings in web.config

Adding and Removing keys from appSettings in web.config

August 12th, 2007 Leave a comment Go to comments

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

}

As Daniel has mentioned in his comments, a real benefit of using the SPWebApplication in your code is that the changes are applied to the farm, since the SPWebApplication represents that virtual web application that lives in the farm context.Thanks Daniel and Tony!

Categories: MOSS 2007 Tags:
  1. No comments yet.
You must be logged in to post a comment.