After passing Microsoft’s exam 70-576 – PRO: Designing and Developing Microsoft SharePoint 2010 Application, I’ve had a real desire to utilize the knowledge learned across my day to day work. One of the common questions I used to keep asking myself was “Where is the best place to store my configuration values?”
Of course, after being through the exam material over and over again, the answer is simple. “It Depends!”
One of the exam objectives in the Managing Application Development section is around defining an application configuration approach. (http://www.microsoft.com/learning/en/us/exam.aspx?id=70-576#tab2) “This objective may include … defining “web.config” modifications, Lists as a configuration option, Property Bags, declarative vs. programmatic, SP persisted objects.
So you are probably reading this with the following burning question:
Where is the best place to store the URL for a master list? or
Where should i store a SQL connection string? or even (but hopefully not)
Where can I store this password?
There’s no one size fits all approach with this, so what I have done is listed some of the more common options, and some (hopefully) helpful information about each.
- web.config File
The web.config file is a popular choice for configuration items, particularly for .NET developers who are starting to ply their trade in SharePoint. Its important to note that SharePoint stores a web.config file for each Web Application (in the [DRIVE]:\inetpub\wss\VirtualDirectories\[WebApplication]\ folder by default). Making a change to the web.config file also (automatically) recycles the associated web application, causing an outage to every site collection hosted on that web application.
Because the web.config file is a physical file on the SharePoint server itself, only an Administrator would be able to make modifications that are required. This is also very relevant when the web application is hosted on multiple servers in a load balanced (or similar) scenario, changes made to one web.config file will only affect processes on that particular server node.
I’ve had some tough times in the past trying to debug issues related to incorrectly formatted web.config files causing an entire web application to be unavailable. Perhaps it could be a reflection of my ability to work with XML documents, but it’s certainly not an option if end-users need to be able to make updates. - Property Bags
When coding with the SharePoint 2010 object model, you’ll find the “Properties” property bag on many classes such as SPSite, SPWeb and more. The property bag is simply a StringDictionary that’s persisted on those objects. What’s important to note is that the property bag doesn’t have any granular security available, but you can manage this by being selective about the scope you choose. You’ll also need to make you keys unique to avoid conflicts with other applications using the same property bag.
You can also go ahead and make a UI for interacting with the Property Bag if you have requirements around users updating the configuration items. I haven’t used it myself, but you may find this codeplex project is a good start: http://pbs.codeplex.com/ - SharePoint Persisted Objects
The SPPersistedObject goes one step further than the Properties Bag and allows you to store objects against the SPFarm object, rather than just strings. You can get a persisted object by using the GetObject method on an SPFarm object, or the GetChild method of the SPPersistedObject class The following MSDN article might be of useful reference: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.sppersistedobject.aspx - SharePoint Lists
The most user-friendly option, and that is important if your end-users need to able to make updates. You’ve got an option here that won’t need a lot of additional development (if any). If you have a set of business rules around the allowable configuration values, you’ve also got a lot of Out Of the Box friendliness right at your fingertips (think field types, list validation, security, etc.).
Hopefully after reading this, you can now make an informed design decision on configuration item storage.