The other day I held a presentation of the Epicode Netmeeting module at the EPiServer Meetup. Settings for this module is stored in its own config section i Web.Config. Stein Viggo asked me if there is another way for storing config settings. And indeed there is. Here is a solution for doing so.

I have a module for showing scaled and cropped pictures. This module has two settings in its own config section:

<ImageResizerSettings>
    <add key="AllowedPictureFormats" value="jpg,jpeg,png,gif,tif,tiff" />
    <add key="ScaledImagesProviderName" value="ScaledImages" />        
</ImageResizerSettings>

I want to store these settings in the dll for this module. Make a class that uses the GuiPlugIn attribute.

using System;
using System.Web.UI.WebControls;
using EPiServer.PlugIn;

namespace Epinova.ImageResizer.Utilities
{
    [GuiPlugIn(Area = PlugInArea.None, DisplayName = "ImageResizerSettings")]
    public class ImageResizerSettings
    {
        private static ImageResizerSettings _instance;

        [PlugInProperty(Description = "Allowed picture formats", AdminControl = typeof(TextBox), AdminControlValue = "Text")]
        public string AllowedPictureFormats { get; set; }

        [PlugInProperty(Description = "The name of the provider where the scaled images are stored", AdminControl = typeof(TextBox), AdminControlValue = "Text")]
        public string ScaledImagesProviderName { get; set; }

        public ImageResizerSettings()
        {
            PlugInSettings.SettingsChanged += new EventHandler(PlugInSettings_SettingsChanged);
        }

        private static void PlugInSettings_SettingsChanged(object sender, EventArgs e)
        {
            _instance = null;
        }

        public static ImageResizerSettings Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ImageResizerSettings();
                }
                PlugInSettings.AutoPopulate(_instance);
                return _instance;
            }
        }
    }
}

Build your solution and go to admin mode and select the "Config tab". Then click the "Plug-in Manager"

Config tab

Select the name of your dll and your configuration settings will appear. Fill in the settings and click "Save".

Settings

Now you can access these settings in code like this:

string filesystemProviderName = ImageResizerSettings.Instance.ScaledImagesProviderName;

private readonly string _allowedExtenstions = ImageResizerSettings.Instance.AllowedPictureFormats;

A common way to store settings is to a PageReference. You can do it in the following way:

[PlugInProperty(Description = "Link to a page", AdminControl = typeof(InputPageReference), AdminControlValue = "PageLink")]
public PageReference LinkToPage{ get; set; }

The PageReference is accessed the same way as the other settings:

ImageResizerSettings.Instance.LinkToPage.

If you want to add a reference to a PageTypeId, you can use this class:

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using EPiServer.DataAbstraction;
using EPiServer.Web.WebControls;

namespace YourNameSpace{
[Serializable, DefaultProperty("PageTypeID"), ToolboxItem(false), ValidationProperty("PageTypeID"), ToolboxData("<{0}:InputPageType runat=server></{0}:InputPageType>")]
    public class InputPageType : InputBase
    {
        DropDownList _ddl = new DropDownList() { DataSource = PageType.List(), DataTextField = "LocalizedName", DataValueField = "ID" };

        public int PageTypeID
        {
            get { int val; int.TryParse(_ddl.SelectedValue, out val); return val; }
            set { _ddl.SelectedValue = value.ToString(); }
        }

        protected override void CreateChildControls()
        {
            _ddl.DataBind();
            Controls.Add(_ddl);
        }
    }
}

The PageTypeId can be references like this in you Settings class:

[PlugInProperty(Description = "Your PageType", AdminControl = typeof(InputPageType), AdminControlValue = "PageTypeID")]
 public int YourPageTypeId{ get; set; }

And you can use it in your code like this:

ImageResizerSettings.Instance.YourPageTypeId

So what do you think, is a good way to store your settings?

Thanks to Thomas Leela for showing me this nice solution!