Monday 19 May 2008

Config Store FAQs

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=1.0.0.0, Culture=neutral, PublicKeyToken=23afbf06fd91fa64" %>
. <!-- normal page markup here -->
.
.
<COB:ConfigTestControl runat="server"></COB:ConfigTestControl>


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.


Anything else?
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.

Sunday 11 May 2008

Introducing the SharePoint Config Store for developers

Today I want to introduce something I've been working on recently which could be of use to you if you're a SharePoint developer. Often when developing SharePoint solutions which require coding, the developer faces a decision about what to do with values he/she doesn't want to 'hardcode' into the C# or VB.Net code. Example values for a SharePoint site/application/control could be:

'AdministratorEmail' - 'bob@somewhere.com'
'SendWorkflowEmails' - 'true'

Generally we avoid hardcoding such values, since if the value needs to be changed we have to recompile the code, test and redeploy. So, alternatives to hardcoding which folks might consider could be:

  • store values in appSettings section of web.config
  • store values in different places, e.g. Feature properties, event handler registration data, etc.
  • store values in a custom SQL table
  • store values in a SharePoint list

Personally, although I like the facility to store complex custom config sections in web.config, I'm not a big fan of appSettings. If I need to change a value, I have to connect to the server using Remote Desktop and open and modify the file - if I'm in a server farm with multiple front-ends, I need to repeat this for each, and I'm also causing the next request to be slow because the app domain will unload to refresh the config. Going through the other options, the second isn't great because we'd need to be reactivating Features/event receiver registrations every time (even more hassle), though the third (using SQL) is probably fine, unless I want a front-end to make modifying the values easier, in which case we'd have some work to do.

So storing config values in a SharePoint list could be nice, and is the basis for my solution. Now I'd bet lots of people are doing this already - it's pretty quick to write some simple code to fetch the value, and this means we avoid the hardcoding problem. However, the Config Store 'framework' goes further than this - for one thing it's highly-optimized so you can be sure there is no negative performance impact, but there are also some bonuses in terms of where it can used from and the ease of deployment. So what I hope to show is that the Config Store takes things quite a bit further than just a simple implementation of 'retrieving config values from a list'.

Details (reproduced from the Codeplex site)

The list used to store config items looks like this (N.B. the items shown are my examples, you'd add your own):

ConfigStoreList.jpg
There is a special content type associated with the list, so adding a new item is easy:

ConfigItemContentType.jpg

..and the content type is defined so that all the required fields are mandatory:

AddNewConfigItem.jpg

Retrieving values


Once a value has been added to the Config Store, it can be retrieved in code as follows (you'll also need to add a reference to the Config Store assembly and 'using' statement of course):

string sAdminEmail = ConfigStore.GetValue("MyApplication", "AdminEmail");


Note that there is also a method to retrieve multiple values with a single query. This avoids the need to perform multiple queries, so should be used for performance reasons if your control/page will retrieve many items from the Config Store - think of it as a best practise. The code is slightly more involved, but should make sense when you think it through. We create a generic List of 'ConfigIdentifiers' (a ConfigIdentifier specifies the category and name of the item e.g. 'MyApplication', 'AdminEmail') and pass it to the 'GetMultipleItems()' method:


List<ConfigIdentifier> configIds = new List<ConfigIdentifier>();

ConfigIdentifier adminEmail = new ConfigIdentifier("MyApplication", "AdminEmail");
ConfigIdentifier sendMails = new ConfigIdentifier("MyApplication", "SendWorkflowEmails");
configIds.Add(adminEmail);
configIds.Add(sendMails);
Dictionary<ConfigIdentifier, string> configItems = ConfigStore.GetMultipleValues(configIds);
string sAdminEmail = configItems[adminEmail];
string sSendMails = configItems[sendMails];

..the method returns a generic Dictionary containing the values, and we retrieve each one by passing the respective ConfigIdentifier we created earlier to the indexer.


Other notes
  • All items are wrapped up in a Solution/Feature so there is no need to manually create site columns/content types/the Config Store list etc. There is also an install script for you to easily install the Solution.

  • Config items are cached in memory, so where possible there won't even be a database lookup!

  • The Config Store is also designed to operate where no SPContext is present e.g. a list event receiver. In this scenario, it will look for values in your SharePoint web application's web.config file to establish the URL for the site containing the Config Store (N.B. these web.config keys get automatically added when the Config Store is installed to your site). This also means it can be used outside your SharePoint application, e.g. a console app.

  • The Config Store can be moved from it's default location of the root web for your site. For example sites I create usually have a hidden 'config' web, so I put the Config Store in here, along with other items. (To do this, create a new list (in whatever child web you want) from the 'Configuration Store list' template (added during the install), and modify the 'ConfigWebName'/'ConfigListName' keys which were added to your web.config to point to the new location. As an alternative if you already added 100 items which you don't want to recreate, you could use my other tool, the SharePoint Content Deployment Wizard at http://www.codeplex.com/SPDeploymentWizard to move the list.)

  • All source code and Solution/Feature files are included, so if you want to change anything, you can

  • Installation instructions are in the readme.txt in the download


Summary

Hopefully you might agree that the Config Store goes beyond a simple implementation of 'storing config values in a list'. For me, the key aspects are the caching and the fact that the entire solution is 'joined up', so it's easy to deploy quickly and reliably as a piece of functionality. Many organizations are probably at the stage where their SharePoint codebase is becoming mature and perhaps based on a foundation of a 'core API' - the Config Store could provide a useful piece in such a toolbox.


You can download the Config Store framework and all the source code from www.codeplex.com/SPConfigStore.