So last week I introduced my Config Store ‘framework’ on Codeplex, which provides a complete solution (including source code!) for storing config values in a SharePoint list, but skipped over some of the details, including the optimizations which I think make it so much better than a simple implementation of this concept. This post aims to shed a bit more light on what the Config Store is about, and hopefully provides some key information to help you work out whether it’s something which would be useful in your projects.
What kind of configuration values can I put in the Config Store?
In short, any value you wish to avoid hardcoding into your SharePoint application’s C#/VB.Net code. Values are stored in the list as strings, but if they are actually integers, booleans, DateTime values etc., you would just cast them as appropriate after retrieval. Note also that we can also store complex data as XML, and either deserialize into a class or otherwise process with standard XML methods.
How do I retrieve config values in code?
For a single value it’s as simple as:
string sAdminEmail = ConfigStore.GetValue("MyApplication", "AdminEmail");
To take advantage of the optimization (described later) for retrieving multiple values in a single control/page/web part/whatever, use the ConfigStore.GetMultipleValues() method – see the Config Store introduction post or Codeplex site for details.
What are the main components?
The main pieces are:
- Infrastructure – the list, site columns, and content type
- Event receivers – responsible for managing the config cache
- Feature and Feature receiver – responsible for provisioning the infrastructure (Feature) and also adding web.config entries and hooking up event receivers (Feature receiver)
- Code – the main class is the ConfigStore class, this has the GetValue() and GetMultipleValues() methods.
How do I install it?
Simply run the install script which will do the work of installing the Solution (.wsp) to your site. Once that’s complete, there are a couple of things to check got installed correctly – a checklist is included in the readme.txt in the download.
Performance is important on our site – how is the Config Store optimized?
There are several levels of optimization – taking these one at a time:
- all values are cached in memory, so in general retrieval is lightning fast and doesn’t require any kind of database lookup.
- even the first ever request for a particular value will be lightning fast, since the value will already be in the cache. This is possible because of the event receiver on the list – when the admin added the config value to the list, we executed some code to proactively add the item to the memory cache at this point. The caveat here of course, is that IISResets/app pool recycles will still clear down the cache (since it is the ASP.Net memory cache), so this only holds for values added since the last reset/recycle. All that said, you could certainly add code to the global.asax file to cache all values automatically after a reset/recycle if you want ultimate performance.
- if a query is required, the retrieval code uses the current SPContext if one is available – this avoids creating an SPSite/SPWeb object unless absolutely necessary. Needless to say, any objects which are created are also disposed.
- the query is performed using a CAML query (SPQuery) as opposed to iterating through the whole config list.
- the GetMultipleValues() method allows the developer to do just that whilst ensuring there is only one underlying query, rather than one for each value retrieved.
Developers familiar with caching will recognise that this alone provides a huge boost to performance.
Where can I retrieve values from in code?
It doesn’t have to be a SharePoint page, control or web part. The framework is designed to work in other scenarios even when there is no SPContext available – examples are event receivers, InfoPath managed code or even outside the SharePoint web app (e.g. console app, custom STSADM command).
I work in a large SharePoint deployment and my admin doesn’t want to deploy assemblies to the GAC or run under Full Trust. Can I still use the Config Store?
By default the Config Store Solution installs the assembly to the GAC. This provides access to the functionality from outside the SharePoint web app as described above. However, it is possible to run the assembly from your site’s bin directory, though remember you will then need to define CAS policy in your web.config code as appropriate.
I’ve installed the Config Store using the supplied .wsp and install script, how can I quickly tell it’s working?
The quickest way is to add the supplied ‘ConfigTestControl’ to one of your pages in SharePoint Designer. You’ll need the Register directive to link the assembly, and also the control declaration:
<%@ Register Tagprefix="COB" Namespace="COB.SharePoint.Utilities" Assembly="COB.SharePoint.Utilities.ConfigStore, Version=188.8.131.52, Culture=neutral, PublicKeyToken=23afbf06fd91fa64" %>
. <!-- normal page markup here -->
How does the Config Store work in terms of permissions? Do I need to apply any permissions to the list to make it work?
No specific permissions need to be configured, unless you choose to lock down write access to the config list beyond the default – this is for the list to inherit the permissions of the root web of your site. This means anyone with ‘Contribute’ permission in this web (for example) will also be able to work with items in the config list. If you do wish to restrict who can modify config values, simply add whatever security you require to the config list – the Config Store framework will still continue to work since the code retrieves values under an admin context, via use of SPSecurity.RunWithElevatedPrivileges().
Our site is an internet site, will the Config Store work with anonymous users?
Yes. Again, since values are actually retrieved under an admin context, it doesn’t matter that your anonymous users do not have direct permissions to read from the list.
It’s worth remembering that we can also take advantage of services offered by the list infrastructure when using the Config Store. Some ideas could be auditing ("we need to log any changes to our app’s config"), alerts ("if something changes, I want to know about it!"), version history ("let’s see what the setting was when the site was having problems") and workflow ("I want all changes to go through me for change management").
The Config Store framework can be downloaded from www.codeplex.com/SPConfigStore.