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:
-
Create the configuration section class
In the class you define the configuration properties that you need.
Inherit fromConfigSection. You must use only primitive types for configuration properties. You can useNameValueCollection, but you cannot have more than oneNameValueCollectionproperty for a configuration element. To use a collection in the configuration class, you useConfigElementDictionary. You can use complex properties withTypeConverterthat serializes to a primitive type. To persist a property in the configuration file, you must add theConfigurationPropertyattribute.IMPORTANT: The
NameValueCollectionmust be namedParameters.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
NameValueCollectionstores collection of properties that you define in the configuration provider. -
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 withConfigElementparameter that is the parent element. You useIsKeyto 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; } } } } -
Register the new configuration
You must register the configuration section class when the application starts - in theGlobal.asaxor when developing a module - in theInitializemethod of the module class.The following code snippet registers the configuration in the
Global.asaxfile: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.