This is a place to record my journey with SharePoint. I realized that I was learning a lot of stuff that other people would probably like to know about (and that I would like to refer back to... I'm getting kinda old and my memory is fading.)

Sunday, June 1, 2008

Managing Key-Value Pair Settings in SharePoint

Recently my team discussed various ways to manage application configuration settings in SharePoint.  We wanted to avoid using Web.Config files for Key-Value Pair data because of the complexity of managing this data in SharePoint.

SharePoint contains a decent API (SPWebConfigModification) for managing web.config settings across the farm.  It works pretty good, but we have had some trouble with it in certain situations.  If you are interested in a good article on SPWebConfigModification check out this one by Mark Wagner

Thankfully the folks on the SharePoint development team delivered just what we needed to have a simple solution for managing key-value pair settings.  It is the SPPropertyBag class.

SharePoint provides a properties collection for SPFarm, SPWebApplication, SPWebService and SPWeb objects (none for SPSite, but site settings can be stored @ SPSite.RootWeb).

The properties collection for SPFarm, SPWebApplication and SPWebService are not really based on the SPPropertyBag class.  The properties collection comes from the derived class SPPersistedObject and is actually a HashTable.  But it works exactly the same as SPPropertyBag.

It is really easy to manage the properties settings.  Here is some sample code to update settings for SPFarm, SPWebApplication or SPWebService.

public static void SetPropertyValue(SPPersistedObject spObject, string name, string value)
{
if (spObject.Properties.ContainsKey(name))
{
spObject.Properties[name] = value;
}
else
{
spObject.Properties.Add(name, value);
}
spObject.Update();
}

The SPWeb class actually uses SPPropertyBag for its properties.  The code to manage SPWeb.Properties is similar.


public static void SetPropertyValue(SPWeb webSite, string name, string value)
{
if (webSite.Properties.ContainsKey(name))
{
webSite.Properties[name] = value;
}
else
{
webSite.Properties.Add(name, value);
}
webSite.Properties.Update();
}

0 comments:

Subscribe to my Blog

Blog Archive

About Me

My Photo
Jeff Dalton
I have been in IT for the past 16 years working with many different technologies and in many different roles. Today my main focus is on moving a CMS 2002 / SharePoint 2003 solution to SharePoint 2007.
View my complete profile