Root Level Web.Config
  <appSettings>
<add key="AppKey" value="AppValue"/> </appSettings>
<add key="AppKey" value="AppValue"/> </appSettings>
Code Snippet
using System;using 
System.Configuration;using 
System.Web.Configuration;
namespace 
SampleApplication.WebConfig
{
public partial class webConfigFile : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
public partial class webConfigFile : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
        {
//Helps to open the Root level web.config file. Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
//Helps to open the Root level web.config file. Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
            //Modifying the AppKey from AppValue to 
AppValue1            webConfigApp.AppSettings.Settings["AppKey"].Value = "AppValue1";
            //Save the Modified settings of 
AppSettings.            webConfigApp.Save();        }
}
}
}
}
Output of Root Level 
Web.Config:
 <appSettings>
<add key="AppKey" value="AppValue1"/> </appSettings>
<add key="AppKey" value="AppValue1"/> </appSettings>
In order to modify application specific web.config entry below is the code snippet:
using System;
using System.Reflection;
using Microsoft.SharePoint.Administration;
namespace SharePointLiveForMe.SharePoint.Administration
{
/// <summary>
/// This class allows to add or remove entries in web.config.
/// </summary>
public class WebConfigManagement
    {
/// <summary>
/// This methods allows to add a key/value pair in the AppSettings section.
/// </summary>
/// <param name="Key">Name of the key</param>
/// <param name="Value">Value</param>
/// <param name="webApp">Web application</param>
public static void AddAppSettingsKeyValue(string key, string value, SPWebApplication webApp)
        {
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value) && webApp != null)
            {
SPWebConfigModification modification = GetModification(key, value);
                //Add app setting key value if it is not there in web.config file 
                if (!webApp.WebConfigModifications.Contains(modification))
                {
webApp.WebConfigModifications.Add(modification);
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApp.Update();
}
else
                {
                    //modify existing entry
                    webApp.WebConfigModifications.Add(modification);
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApp.Update();
}
}
            else
throw new ArgumentNullException();
}
/// <summary>
/// This methods allows to remove a key/value pair in the AppSettings section.
/// </summary>
/// <param name="key">Name of the key</param>
/// <param name="value">Value</param>
/// <param name="webApp">Web application</param>
public static void RemoveAppSettingsKeyValue(string key, string value, SPWebApplication webApp)
        {
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value) && webApp != null)
            {
SPWebConfigModification modification = GetModification(key, value);
                if(webApp.WebConfigModifications.Contains(modification))
                {
webApp.WebConfigModifications.Remove(modification);
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApp.Update();
}
}
            else
throw new ArgumentNullException();
}
/// <summary>
/// This method allows to build an SPWebConfigModification object.
/// </summary>
/// <param name="key">Name of the key</param>
/// <param name="value">Value</param>
/// <returns></returns>
private static SPWebConfigModification GetModification(string key, string value)
        {
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
            {
                SPWebConfigModification modification = new SPWebConfigModification();
modification.Name = string.Format(@"add[@key=""{0}""]", key);
                modification.Path = "configuration/appSettings";
modification.Value = string.Format(@"<add key=""{0}"" value=""{1}"" />", key, value);
                modification.Owner = Assembly.GetExecutingAssembly().FullName;
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                return modification;
}
            else
throw new ArgumentNullException();
}
}
}
 
No comments:
Post a Comment