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:
- SPWeb.AddProperty(object key, object value)
- SPWeb.DeleteProperty(object key)
- SPWeb.GetProperty(object key)
- SPWeb.SetProperty(object key, object value)
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: