Welcome to the navigation

Elit, proident, reprehenderit in consequat, lorem veniam, exercitation anim ipsum nulla qui dolor nostrud ut sunt do dolore ut amet, ad tempor fugiat minim laborum. Duis ut id quis ut dolore reprehenderit non aute ullamco dolor voluptate sed nulla excepteur laboris amet, minim ut occaecat enim incididunt in sint velit

Yeah, this will be replaced... But please enjoy the search!

Howto programmatically add, update and remove ConnectionStrings in Umbraco

Categories Tags
I found that amazing no-one ever posted snippets to howto actually manipulate the web.config file when using Umbraco. Anyway, lets keep it short. Dont forget to import the businesslogic.dll into your project and reference it
using System;
using System.Configuration;
using umbraco; // from businesslogic.dll

 

Adding a connectionString in web.config

Note, you should either replace the Camelot.SharePointProvider with your preferred provider or add a parameter to the method to be able to modify this dynamically.
/// <summary>
/// Adds a new connectionString in web.config.
/// </summary>
/// <param name="name">Name the connection</param>
/// <param name="connString">The actual connectionstring. Read more at http://docs.bendsoft.com/camelot-net-connector/</param>
public static void AddConnection(string name, string connString)
{
    var webConfig = new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot + "web.config" };
    var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
    config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(name, connString, "Camelot.SharePointProvider"));
    config.Save();

    ConfigurationManager.RefreshSection("connectionStrings");
}

Updating a connectionString in web.config

Note, if the connectionString doesn't exist this method will create it for you
/// <summary>
/// Update an existing connectionString, if it dosn't exist it will be created
/// </summary>
/// <param name="name">Name the connection</param>
/// <param name="connString">The actual connectionstring. Read more at http://docs.bendsoft.com/camelot-net-connector/</param>
public static void UpdateConnection(string name, string connString)
{
    var webConfig = new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot + "web.config" };
    var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
    var section = (ConnectionStringsSection)config.GetSection("connectionStrings");

    if (section.ConnectionStrings[name] != null)
    {
        // Update the connectionstring if it exist
        section.ConnectionStrings[name].ConnectionString = connString;
        config.Save();

        ConfigurationManager.RefreshSection("connectionStrings");
    }
    else
    {
        // ...otherwise we add a new
        AddConnection(name, connString);
    }
}

Delete a connectionString in web.config

/// <summary>
/// Remove a connectionString
/// </summary>
/// <param name="name">The name of the connectionString to remove</param>
public static void RemoveConnection(string name)
{
    var webConfig = new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot + "web.config" };
    var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
    var section = (ConnectionStringsSection)config.GetSection("connectionStrings");

    // Die if the connectionstring dosn't exist
    if (section.ConnectionStrings[name] == null) return;

    var keys = section.ConnectionStrings;
    keys.Remove(name);
    config.Save();
    ConfigurationManager.RefreshSection("connectionStrings");
}

Check if connectionstring exist

/// <summary>
/// Check if a connectionString with the same name already exist
/// </summary>
/// <param name="name">Name of the connection</param>
/// <returns>True on existence, otherwise false</returns>
private static bool ConnectionExist(string name)
{
    var webConfig = new ExeConfigurationFileMap { ExeConfigFilename = GlobalSettings.FullpathToRoot + "web.config" };
    var config = ConfigurationManager.OpenMappedExeConfiguration(webConfig, ConfigurationUserLevel.None);
    var section = (ConnectionStringsSection)config.GetSection("connectionStrings");

    // Return false if it doesn't exist, true if it does
    return section.ConnectionStrings[name] != null;
}
Thats it!
Please note that this portal is a BETA site , I will continuously improve all functionality and performance during the coming weeks / the author