SPG: Add settings to SPWebApplication or SPFarm

If you are using the Patterns and Practices SharePoint Guidance library (SPG) to add application settings to SharePoint, like I do, and you are using the 2010 version of it there are some differences to the API that you might not be aware of as they are not so well documented in the included .chm file.

 

In 2007

When using the IConfigManager from SPG in 2007 to add a setting, the SetInPropertyBag method had four overloads looking like this:

void SetInPropertyBag(string key, object value, SPFarm propertyBag);
void SetInPropertyBag(string key, object value, SPWebApplication propertyBag);
void SetInPropertyBag(string key, object value, SPSite propertyBag);
void SetInPropertyBag(string key, object value, SPWeb propertyBag);

You use the one that matches, passing in an instance of SPFarm/SPWebApplication/SPSite/SPWeb, and your setting will be added at the correct scope.

 

In 2010

When using the IConfigManager from SPG in 2010 to add a setting, the SetInPropertyBag method only has one signature looking like this:

void SetInPropertyBag(string key, object value, IPropertyBag propertyBag);

Now that method is expecting an instance of something called an IPropertyBag.
So how do we get a hold of that, and more interesting how does that sync up with the desired scope of the setting?

The IConfigManager has a method called GetPropertyBag to return an instance of that IPropertyBag. That method wants an enumeration (ConfigLevel) value to determine which scope to return. The problem is that it uses SPContext.Current.Web internally to determine the starting point.

A starting point?
Yes, it goes up the hierarchy from the SPWeb until it reaches the desired scope. If we were to use the SPWebApplication level in a feature receiver we will not have SPContext, so how to use it then?

IConfigManager also has a method that allows us to set the SPWeb to use as a starting point called SetWeb. Again, if a feature is scoped to SPWebApplication how do we get an instance of SPWeb to pass into that method?

It´s not really that complicated though, you just create an instance of IPropertyBag that meets your scope so for SPWebApplication, like this:

SPWebApplication instance = GetThatInstance();
IPropertyBag properties = new SPWebAppPropertyBag(instance);

SPG has property bags for Farm, Web Application, Site Collection, and Site that you can use.
To be a little convenient I made an extension method on each of those “types” that returns the IPropertyBag for that scope. So for SPWebApplication that looks like:


public static IPropertyBag PropertiesBag(this SPWebApplication instance)
{
IPropertyBag properties = new SPWebAppPropertyBag(instance);
return properties;
}

And you could use it something like this:


SPWebapplication MyWebApplicationInstance = GetThatWebApplicationInstance();
IConfigManager ConfigManager = GetThatConfigManagerInstance();

ConfigManager.SetInPropertyBag("key", "value", MyWebApplicationInstance.PropertiesBag());

, , , , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: