Skip to content

Storing configuration

takerusilt edited this page Aug 1, 2016 · 1 revision

Sometimes you need to have some configurations to your code deployed to SharePoint. The library provides a way to allow control to these configuration based on a SharePoint list, instead of what might be nasty <appConfig> or a custom application page.

public class MyConfig : SiteConfig<MyConfig> {
    public bool FeatureOn { get; set; }
}

A SharePoint defaults to location /Lists/SiteConfig will be automatically created with you entries as list items.

The object instance of your configuration class is cached per site collection. Changes to those list items will invalidate the cache on all machine within seconds without extra efforts.

To get the values of the current site in an HTTP request simply call

MyConfig config = MyConfig.Current;

To get values for a specific site collection or you want to flush the cache manually you can call

MyConfig config = MyConfig.Load(site);
MyConfig config = MyConfig.Load(site, true); // flush cache

Supported property types

All primitive types are supported.

In addition, the following types are also supported without a type converter:

  • StringCollection: each non-empty line will be treated as an entry in the collection.
  • SecureString: value will be stored in Secure Store Service.

For other types, you will need to have a TypeConverter class to handle the deserialization.

public class MyConfig : SiteConfig<MyConfig> {
    [TypeConverter(typeof(IniConfigurationTypeConverter))]
    public IniConfiguration CompositeConfig { get; set; }
}

Attributes to properties

The following attributes can be decorated on the properties:

  • DefaultValueAttribute
  • CategoryAttribute
  • DescriptionAttribute

If DefaultValueAttribute is decorated, the list item representing that property will have the default value assigned when created, or restored when user re-checked the Use Default Value on the list item.

Separate different configurations

There may be needs to store configurations of different kinds in different location, or to have different permissions.

You can define your custom provider to get maximum control to the list created for a specific class containing certain configuration entries.

[SiteConfigProvider(typeof(CustomSiteConfigProvider))]
public class CmsConfiguration : SiteConfig<CmsConfiguration> {
    private class CustomSiteConfigProvider : SiteConfigProvider {
        protected override SPListAttribute InitializeListSettings(SPListAttribute attribute) {
            attribute.Title = "Custom Configurations";
            attribute.Url = "Lists/CustomConfig";
            return attribute;
        }
    }
}

Additional support

Since SharePoint is too smart on dealing URLs that appears in list item (mainly for interoperability between request to different zones), the configuration list is crafted so that the URLs stored in the list item will remain constant whenever which zone visiter is requesting.