Reza on blogging [MVP]

THIS BLOG HAS MOVED TO: http://blogs.devhorizon.com/reza

Subscriptions

<March 2010>
SuMoTuWeThFrSa
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

News



toronto.sharepoint.camp


Navigation

Post Categories

Other Bloggers

Personal Links

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


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!


posted on Sunday, August 12, 2007 10:03 PM by rezaa

Powered by Community Server, by Telligent Systems