Creating configuration classes

Sitefinity allows you to create your own configuration classes.

This section contains the following:

What are configuration properties, sections and elements?

The configuration section is a section in a configuration file, for example web.config. The configuration element represents an XML element in a configuration file. It can contain zero or more child elements. The configuration property represents an attribute of an element.

In Sitefinity, ConfigSection represents a configuration section, and ConfigElement - represents a configuration element within a configuration file.

Creating new configuration

In this example you create a page appearance configuration that stores page settings like fonts and color.

To create the configuration, you must perform the following steps:

  1. To create the configuration section, you inherit from ConfigSection. In the class you define the configuration properties that you need. You must use only primitive types for configuration properties. Sitefinity allows you to use NameValueCollection, but you cannot have more than one NameValueCollection property for a configuration element. To use a collection in the configuration class, you useConfigElementDictionary. 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.

    The following code creates the configuration section class:

    public class PageAppearanceConfig : ConfigSection
    {
        [ConfigurationProperty( "remoteOnly", DefaultValue = true, IsRequired = true )]
        public bool RemoteOnly
        {
            get
            {
                return ( bool )this[ "remoteOnly" ];
            }
            set
            {
                this[ "remoteOnly" ] = value;
            }
        }
     
        [ConfigurationProperty( "parameters" )]
        public NameValueCollection Parameters
        {
            get
            {
                return ( NameValueCollection )this[ "parameters" ];
            }
            set
            {
                this[ "parameters" ] = 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.

    For more information about creating a type converter, see How to: Implement a Type Converter - MSDN.

  2. 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 useIsKey to define a key for the class.

    The following code creates the font and color elements:

    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;
            }
        }
    }

Registering new configuration

You must register the configuration section class when the application starts - in Global.asax, or, when developing a module, in the Initialize method of the module class.

protected void Application_Start(object sender, EventArgs e)
{
    Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized);
}
  
void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
{
    Config.RegisterSection< PageAppearanceConfig >();
}

Reading and modifying configuration

To read and modify configuration, you use Config and ConfigManager.

ConfigManager reads configurations from folder ~/App_Data/Sitefinity/Configuration. The configuration section is stored in a file with the same name as the configuration class. The extension is .config. The file is created only if the configuration differs from the default values.

NOTE: To create the file, you can copy and modify an existing configuration file.

To define the page appearance, create PageAppearanceConfig.config file and paste the following code in it:

<?xml version="1.0" encoding="utf-8"?>
<pageAppearanceConfig xmlns:config="urn:telerik:sitefinity:configuration" xmlns:type="urn:telerik:sitefinity:configuration:type" config:version="4.2.1641.0" remoteOnly="False" country="USA">
      <color background="008000" foreground="D3D3D3"/>
    <Fonts>
        <add size="18" name="Verdana" />
    </Fonts>
</pageAppearanceConfig>

To read values from PageAppearanceConfig.config, use the following code:

PageAppearanceConfig config = Config.Get<PageAppearanceConfig>();
var pageColor = config.Color;
var pageFontSize = config.Fonts[ "Verdana" ].Size;

To modify values from PageAppearanceConfig.config, use the following code:

ConfigManager manager = ConfigManager.GetManager();
PageAppearanceConfig config = manager.GetSection<PageAppearanceConfig>();
 
config.Fonts.Add( new FontElement( config.Fonts )
{
    Name = "TimesNewRoman",
    Size = 16,
} );
 
manager.SaveSection( config );

After you register the configuration, Sitefinity allows you to modify it in the Advanced Settings tab in the backend.

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK