-
Notifications
You must be signed in to change notification settings - Fork 2
Storing configuration
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
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; }
}
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.
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;
}
}
}
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.
-
Working with list items
-
Advanced model class usage
-
Other features