Welcome to the navigation

Proident, dolore incididunt in ipsum eiusmod esse occaecat aliquip est veniam, dolor dolor officia ad adipisicing enim sit ullamco nulla do sed sint laboris velit. Et proident, qui voluptate sunt ut ipsum nostrud cillum occaecat commodo consequat, eiusmod minim in mollit quis ut est consectetur dolore sit deserunt laborum, ullamco

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!