February 26, 2009

How to add or remove appsettings key/value pair in the web.config of a SharePoint web application programmatically

To add or remove an appsettings key/value pair in the web.config of a SharePoint web application, you have use the Add or Remove methods of the WebConfigModifications property of an instance of the SPWebApplication class. These two methods take a SPWebConfigModification object as parameter.

I build a class (WebConfigManagement) containing static methods to help you to add or remove appsettings key/value pair:


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);
                if (!webApp.WebConfigModifications.Contains(modification))
                {
                    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();
        }
    }
}

Hereunder is a little console application to show how to use the WebConfigManagement class:

using System;
using Microsoft.SharePoint.Administration;
using SharePointLiveForMe.SharePoint.Administration;
 
namespace Test.SharePointLiveForMe.SharePoint.Administration
{
    class Program
    {
        static void Main(string[] args)
        {
            SPFarm farm = SPFarm.Local;
            SPWebService service = farm.Services.GetValue<SPWebService>("");
            SPWebApplication myWebApplication = null;
            foreach (SPWebApplication webApplication in service.WebApplications)
            {
                if (webApplication.DisplayName == "My Web Application")
                    myWebApplication = webApplication;
            }
 
            Console.WriteLine("Write AppSettings Key/Value");
            WebConfigManagement.AddAppSettingsKeyValue("IsCool", "true", myWebApplication);
            Console.ReadLine();
 
            Console.WriteLine("Remove AppSettings Key/Value");
            WebConfigManagement.RemoveAppSettingsKeyValue("IsCool", "true", myWebApplication);
            Console.ReadLine();
        }
    }

February 11, 2009

Where to find the connection string of the SharePoint Configuration Database

The connection string is located in the Windows registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\12.0\Secure\ConfigDb with dns as key name. Click on the following picture to enlarge

February 10, 2009

Microsoft TechDays in Belgium, 10-12 March 2009



During 3 days, in 60 technical sessions, you’ll get a deep-dive on existing and future technologies of Microsoft:

- Discoveries: Windows 7, Windows Azure, Cloud Computing, C# 4.0
- Architecture and development: Visual Studio, Silverlight, SharePoint, web applications, …
- Infrastructure and enterprise applications: SQL Server, WCF, Windows Server, virtualization, System Center, security, …
- Personal and collective efficiency: Data access, desktop management, Unified Communications, remote office operations, SharePoint,…

But also:

- Get your hands on a Microsoft Surface
- Interact with the Microsoft partner network and the Microsoft linked applications and services
- Discover our certification offerings
- Share impressions with your peers and meet with internationally renowned professionals

You can also deepen your skills during one of the optional dedicated preconference tracks on March 10.

Here is the registration link:
http://www.microsoft.com/belux/techdays/registration.aspx