Pretty much every enterprise application uses configuration settings of some sort, managing these settings across several environments can be a challenging task. Luckily Patterns and Practices SPG 2010 package provides us plenty of guidance when designing applications or customizations that run on top of SharePoint Server 2010 as well as some foundation classes. SPG 2010 from Patterns and Practices is a must read for every SharePoint developer, so if you are not familiar with it head over to codeplex site right now and add a task in your outlook 2010 to get familiar with it. In this article I will show how to integrate application settings manager into your build process to setup environment specific settings.
This is a helper class I wrote to wrap all calls to SPG application settings manager, there are couple of reasons behind this
- I needed a simple interface to call from PowerShell script
- My developers need not necessary have to know how to use the application settings manager
- It provides me an option to swap out the Application Settings Manager with some other solution incase we run into performance or other issues with out impacting application code as well as my development team
This helper class has 3 methods
|AddSetting(string key, object value)||Adds a specific setting to property bag using the application settings manager|
|GetValue<TValue>(string key)||Gets value for setting using the application settings manager|
|RemoveSetting(string key)||Removes a specific setting from property bag using the application settings manager|
Another thing I want to point out is since the SPG Application settings manager expects a “SPContext” which is not going to be there when its invoked from PowerShell script, in “AddSetting” method check to see if SPContext exists if yes then SPSite reference is obtained from it otherwise the code instantiates SPSite using the url passed via constructor and use the SetWeb method on ConfigManager class to provide the Application Settings Manager a web context. If you don’t do this you will get exception when there is no SPContext
Define all your environment specific application configuration settings in an xml file, create one file for each environment you have in your scenario, here is how the XML file looks like. For the purpose of this article, in the sample xml file I have just one setting called “WS.Url”, but in real world scenarios you will have more than just one.
Assuming you all are using PowerShell scripting for deploying your WSP’s, script below will read the xml file and for each setting define it will call the “AddSetting” method to set environment specific settings
I wrote a sample WebPart just to show how application could be consuming these environment specific settings set via your deployment PowerShell script
There you have it, a simple approach to integrating updating environment specific application settings into your build process. Download SPG 2010 from CodePlex and start building some cool applications on SharePoint 2010