SPG: Find all settings using the application settings manager

If you are using the The Application Setting Manager from Patterns and Practices SharePoint Guidance library (SPG) to work with application settings, like I do, you might have noticed that the IConfigManager really has no method for retrieving all the settings at a given scope. You can only add, delete and retrieve one setting.

So, the best idea that I could come up with was to overcome that by exposing them through extension methods on the SPWeb, SPSite, SPWebApplication, and SPFarm objects.
Here is how that would look:

 

SPWeb and SPSite

Since the settings at these two levels are stored in the SPWeb.AllProperties collection (SPSite uses the RootWeb) you can just return those, like this:

return from DictionaryEntry property in web.AllProperties … // return them anyway you like.

SPWebApplication and SPFarm

You would think that you can use the properties collection at these two levels as well to get those setting…but, no using the property bags at these levels returns no settings at all.

SPG instead uses two unique classes to store settings at these levels:

  • WebAppSettingStore
  • FarmSettingStore

These two derive from SPPersistedObject and hence store themselves in the database. To get the settings at the web application level you would have to do something like this:

WebAppSettingStore ConfigurationStore = WebAppSettingStore.Load(webapplication);
return from setting in ConfigurationStore.Settings …// return them anyway you like.

The code uses the WebAppSettingsStore.Load and passes in the webapplication instance to retrieve the persisted instance of WebAppSettingsStore. Then use the WebAppSettingsStore.Settings property to get all the settings at this level.

The same code is used when going against the SPFarm level but using the FarmSettingStore instead.

 

 

Point of interest

One thing to notice here is when you use SPFarm and SPWebApplication you only get the settings stored by SPG (since a separate SPPersistedObject is used).
Although, when you use AllProperties, like with SPSite and SPWeb you will get all the properties including those stored by SharePoint itself. If you wanted to get only those stored by SPG you could do it like this:

 

SPSite

 return (from DictionaryEntry property in site.AllProperties
where property.Key.ToString().StartsWith(SPG.PNPNamespacePrefix) &&
property.Key.ToString().EndsWith(SPG.PNPSiteCollectionNamespaceSuffix)
select property)
.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString());

SPWeb

 return (from DictionaryEntry property in site.AllProperties
where property.Key.ToString().StartsWith(SPG.PNPNamespacePrefix)
select property)
.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString());

Explanation

These two code sections both uses AllProperties on the SPWeb (SPSite uses RootWeb) and since SPG always adds a prefix (and in when the settings are scoped for SPSite also a suffix) we will only returns those that have the prefix (and suffix if SPSite).

SPG. PNPNamespacePrefix and SPG.PNPSiteCollectionNamespaceSuffix are just fancy wrappers around a couple constants in the SPG namespace that look like this:

 public static string PNPNamespacePrefix
 {
 get
 {
 return string.Concat(ConfigManager.PnPKeyNamespace, ".");
 }
 }

 public static string PNPSiteCollectionNamespaceSuffix
 {
 get
 {
 return SPSitePropertyBag.KeySuffix;
 }
 }
Advertisements

, , , , , , ,

  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: