title: Umbraco - Create Configuration Section at runtime with config source
body: <p>I'm developing an umbraco package which uses custom configuration.</p>
  
  <p>My need is to create the configuration section during the intall process.</p>
  
  <p>With packageActionsContrib it works fine, the code add a configSection as needed and creates a node:</p>
  
  <pre><code>&lt;section name="LogServiceConfiguration" type="Utils.LogServiceConfiguration, Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" overrideModeDefault="Allow" restartOnExternalChanges="true" requirePermission="true" /&gt;
  
  ...
  
  &lt;LogServiceConfiguration&gt;
      &lt;Error EmailNodeId="0" PageUrl="" DoRedirect="false" /&gt;
    &lt;/LogServiceConfiguration&gt;
  </code></pre>
  
  <p>What I need is to change the second part with</p>
  
  <pre><code>&lt;LogServiceConfiguration configSource="config\LogService.config" /&gt;
  </code></pre>
  
  <p>I already provide the file with installation, so I have only create the line, not the file.</p>
  
  <p>The code is this</p>
  
  <pre><code>public class AddConfigurationSection : IPackageAction
  {
      #region IPackageAction Members
  
      public bool Execute(string packageName, XmlNode xmlData)
      {
  
          try
          {
              var config = WebConfigurationManager.OpenWebConfiguration("~");
              var sectionName = xmlData.SelectSingleNode("//Section").Attributes["name"].Value;
  
              if (config.Sections[sectionName] == null)
              {
                  var assemblyName = xmlData.SelectSingleNode("//Section").Attributes["assembly"].Value;
                  var typeName = xmlData.SelectSingleNode("//Section").Attributes["type"].Value;
                  var assembly = Assembly.Load(assemblyName);
  
                  if (assembly == null) return false;
  
                  var configSection = assembly.CreateInstance(typeName) as ConfigurationSection;
  
                  if (configSection == null) return false;
  
                  config.Sections.Add(sectionName, configSection);
                  configSection.SectionInformation.ForceSave = true;
                  config.Save(ConfigurationSaveMode.Full);
              }
  
              return true;
          }
          catch (Exception e)
          {
              string message = "Error at execute AddConfigurationSection package action: " + e.Message;
              Log.LogException(message, e);
              return false;
          }
      }
  
      public string Alias()
      {
          return "AddConfigurationSection";
      }
  
      public bool Undo(string packageName, XmlNode xmlData)
      {
          try
          {
              var config = WebConfigurationManager.OpenWebConfiguration("~");
              var sectionName = xmlData.SelectSingleNode("//Section").Attributes["name"].Value;
  
              if (config.Sections[sectionName] != null)
              {
  
                  config.Sections.Remove(sectionName);
                  config.Save(ConfigurationSaveMode.Full);
              }
              return true;
          }
          catch
          {
  
              return false;
          }
      }
  
      public XmlNode SampleXml()
      {
          var sample = "&lt;Action runat=\"install\" undo=\"true\" alias=\"AddConfigurationSection\"&gt;&lt;Section name=\"\" assembly=\"\" type=\"\" /&gt;&lt;/Action&gt;";
          return helper.parseStringToXmlNode(sample);
      }
  
      #endregion
  }
  </code></pre>