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()
Advertisements

One thought on “Working with SPWeb’s Propertybag

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s