For the most part we normally like to keep our application settings in the web.config file nicely stashed away under the appSettings node. But, a change to any of these settings will restart the application, which in most cases is not the desired effect.

A solution to this problem is to create your own configuration section which you can manage in the Sitefinity backend under Administration > Settings > Advanced settings.

This is a lot simpler than you might think.

You can either do this using Sitefinity Thunder or you can do it from scratch. In either case there is a bit of manual work required.

The key parts to the puzzle is a class file describing the available settings and a couple of lines of code in the Global.asax.cs file to register it.

First the class. I generally keep my Sitefinity customizations in a folder called /Extensions, but you can name it whatever suites your work environment. In this folder I create a sub-folder for a specific type of customization, in this case I call it CustomConfigSections.

If you’re using Sitefinity Thunder, right click the CustomConfigSections folder and click Add New Item. In the Add New Item dialog select Sitefinity then Sitefinity Configuration Section, give it a name e.g. ‘MyCompanyConfig.cs‘ and click Add.

Sitefinity Thunder Custom Config Section

The class file you end up with looks as follows:

using System;
using System.Configuration;
using System.Linq;
using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Localization;

namespace SitefinityWebApp.Extensions.CustomConfigSections
{
    [ObjectInfo(Title = "MyCompanyConfig Title", Description = 
    "MyCompanyConfig Description")]
    public class MyCompanyConfig : ConfigSection
    {
        [ObjectInfo(Title = "Text", Description = 
        "This is a sample string field.")]
        [ConfigurationProperty("Text", DefaultValue = "Hello, World!")]
        public string ExampleConfigProperty
        {
            get
            {
                return (string)this["Text"];
            }
            set
            {
                this["Text"] = value;
            }
        }
    }
}

With this file you already have a working configuration section, but to get it to show up in Sitefinity’s backend you need to register it in the global.asax first.

If you don’t already have a global.asax file in your project, add one now and then add the following:

using SitefinityWebApp.Extensions.CustomConfigSections;
using System;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Configuration;

namespace SitefinityWebApp
{
     public class Global : System.Web.HttpApplication
     {
         protected void Application_Start(object sender, EventArgs e)
         {
             Bootstrapper.Initialized += Bootstrapper_Initialized;
         }

         private void Bootstrapper_Initialized(object sender, 
         Telerik.Sitefinity.Data.ExecutedEventArgs e)
         {
             Config.RegisterSection();
         }
     }
}

The important bits are in bold. Once you’ve registered the custom Sitefinity Config Section, build your project, refresh the site and then head over to the Administration > Settings > Advanced Settings section then scroll down to MyCompany and there you should see one property called ExampleConfigProperty.

Once you’ve saved a value in that property you can head over to the project folder in File Explorer and open App_Data/Sitefinity/Configuration, there you should see a new config filed called MyCompanyConfig.config.

Note: This file will not appear in this folder until you save it in the Sitefinity backend for the first time.

As you can see by the file produced by Sitefinity Thunder, there’s not much to the class file and you could have just as well created a standard class file, because Thunder doesn’t automatically register it in the global.asax file.

The key benefits to this approach is that you can manage settings, that you would normally put into the web.config file, in Sitefinity itself without causing an application restart.

In my next installment I’ll get into how to make use of the settings stored in this config file.