Create a new configuration

In this example, you create a page appearance configuration that stores page settings, such as fonts and colors.

To create the configuration, you perform the following:

  1. Create the configuration section class
    In the class you define the configuration properties that you need.
    Inherit from ConfigSection . You must use only primitive types for configuration properties. You can use NameValueCollection , but you cannot have more than one NameValueCollection  property for a configuration element. To use a collection in the configuration class, you use ConfigElementDictionary . You can use complex properties with TypeConverter  that serializes to a primitive type. To persist a property in the configuration file, you must add the ConfigurationProperty  attribute.

    IMPORTANT: The NameValueCollection must be named Parameters.

    The following code creates the configuration section class:

    C#
    using System;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Configuration;
    using Telerik.Sitefinity.Configuration;
    using Telerik.Sitefinity.Localization;
    using Telerik.Sitefinity.Utilities.TypeConverters;
    using Telerik.Sitefinity.Web.Configuration;
    
    namespace SitefinityWebApp
    {
        public class PageAppearanceConfig : ConfigSection
        {
            [ConfigurationProperty("remoteOnly", DefaultValue = true, IsRequired = true)]
            public bool RemoteOnly
            {
                get
                {
                    return (bool)this["remoteOnly"];
                }
                set
                {
                    this["remoteOnly"] = value;
                }
            }
    
            [ConfigurationProperty(ConfigElement.ParametersPropertyName)]
            public NameValueCollection Parameters
            {
                get
                {
                    return (NameValueCollection)this[ConfigElement.ParametersPropertyName];
                }
                set
                {
                    this[ConfigElement.ParametersPropertyName] = value;
                }
            }
    
            [ConfigurationProperty("color")]
            public ColorElement Color
            {
                get
                {
                    return (ColorElement)this["color"];
                }
                set
                {
                    this["color"] = value;
                }
            }
    
            [ConfigurationProperty("Fonts")]
            public ConfigElementDictionary<string, FontElement> Fonts
            {
                get
                {
                    return (ConfigElementDictionary<string, FontElement>)this["Fonts"];
                }
            }
    
            [ConfigurationProperty("version", DefaultValue = null)]
            [TypeConverter(typeof(StringVersionConverter))]
            [ObjectInfo(typeof(ConfigDescriptions), Title = "VersionAttributeTitle", Description = "VersionAttributeDescription")]
            public Version Version
            {
                get
                {
                    return (Version)this["version"];
                }
                set
                {
                    this["version"] = value;
                }
            }
        }
    }

    You represent the page appearance with a collection of fonts and a color. You define a property for controlling whether the appearance is valid only for remote connection. The NameValueCollection stores collection of properties that you define in the configuration provider.

  2. Create the configuration elements.

    To create the configuration elements that represent the font and the color, you inherit from ConfigElement. You must create a constructor with ConfigElement parameter that is the parent element. You use IsKey to define a key for the class.

    The following code creates the font and color elements:

    C#
    using System;
    using System.Configuration;
    using Telerik.Sitefinity.Configuration;
    
    namespace SitefinityWebApp
    {
        public class FontElement : ConfigElement
        {
            public FontElement(ConfigElement parent)
                : base(parent)
            {
            }
    
            [ConfigurationProperty("name", DefaultValue = "Arial", IsRequired = true, IsKey = true)]
            public String Name
            {
                get
                {
                    return (String)this["name"];
                }
                set
                {
                    this["name"] = value;
                }
            }
    
            [ConfigurationProperty("size", DefaultValue = "12", IsRequired = false)]
            public int Size
            {
                get
                {
                    return (int)this["size"];
                }
                set
                {
                    this["size"] = value;
                }
            }
        }
    
        public class ColorElement : ConfigElement
        {
            public ColorElement(ConfigElement parent)
                : base(parent)
            {
            }
    
            [ConfigurationProperty("background", DefaultValue = "FFFFFF", IsRequired = true, IsKey = true)]
            public String Background
            {
                get
                {
                    return (String)this["background"];
                }
                set
                {
                    this["background"] = value;
                }
            }
    
            [ConfigurationProperty("foreground", DefaultValue = "000000", IsRequired = true)]
            public String Foreground
            {
                get
                {
                    return (String)this["foreground"];
                }
                set
                {
                    this["foreground"] = value;
                }
            }
        }
    }
  3. Register the new configuration
    You must register the configuration section class when the application starts - in the Global.asax or when developing a module - in the Initialize method of the module class.

    The following code snippet registers the configuration in the Global.asax file:

    C#
    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)
            {
                ObjectFactory.Initialized += ObjectFactory_Initialized;
            }
    
            private void ObjectFactory_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
            {
                if (e.CommandName == "RegisterConfigurationClasses")
                {
                     Config.RegisterSection<PageAppearanceConfig>();
                }
            }
        }
    }

After you register the configuration, Sitefinity CMS creates a file PageAppearanceConfig.config that is stored in ~/App_Data/Sitefinity/Configuration and it allows you to modify it from the backend. To do this, navigate to Administration» Settings» Advanced.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?