Dynamic Settings Project

Another year, another project! Today I started working on a solution which has the capability to dynamically add and remove properties to and from the SPWeb.AllProperties propertybag. Thanks to a friend of mine for the idea!

The solution can be found on codeplex.

A screenshot of the current settings page (needs some work :-)):

dynamicsettingspage

Working with SPWeb’s Propertybag

The most common scenario for working with site settings is using the site’s propertybag. When you look at the SPWeb’s members, you might notice the following properties and methods:

It’s important to note that the methods of SPWeb that handle properties, reflect upon the SPWeb.AllProperties property and not on the SPWeb.Properties property. Only the AllProperties property should be used, the Properties property is added to SharePoint 2010 in order to provide backwards compatibility with older applications.

A big improvement of the SPWeb.AllProperties is that instead of a StringDictionary, you get a HashTable where you can store objects instead of strings. There is a restriction here though: Only String, INT, and DateTime datatypes can be used as the value in AllProperties.

An example of using SPWeb properties in PowerShell, the same can be used in C#:

$web = Get-SPWeb http://sharepoint
$web.AddProperty("SPWebObjectKey", $web)
$web.Update()
$tempWeb = $web.GetProperty("SPWebObjectKey")
Write-Host $tempWeb.Url

This outputs to http://sharepoint

Now that we have used our property and no longer need it, it’s time to delete it:

$web.DeleteProperty("SPWebObjectKey")
$web.Update()