CSC510240. Do not use SPWeb.Properties collection.

Description

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.
SPWeb.Properties is a StringDictionary, and doesn’t support casing for keys/values (everything gets converted to fully lowercase).

Resolution

Instead of working directly with the SPWeb.Properties, SharePoint 2010 provides SPWeb.AllProperties collection. Although SPWeb.AllProperties is not supported in Sandbox, you can read/write to the property bag through four methods below:
  • SPWeb.GetProperty(Object key) This method retrieves the value of the specified property from the AllProperties property that is a key/value pair.
  • SPWeb.AddProperty(Object key, Object value) This method adds a property to the AllProperties property that is a key/value pair.
  • SPWeb.SetProperty(Object key, Object value) This method updates the value of the specified property in the AllProperties property.
  • SPWeb.DeleteProperty(Object key) This method deletes a property from the AllProperties property that is a key/value pair.
Don’t let the “object” method signature fool you. After adding a boolean value to SPWeb.AllProperties, but when it was later retrieved, it return back as a String instead of a boolean. It’s definitely good to keep in mind that AllProperties might not accept your complex custom object type.
As a final note, just like any other property of SPWeb, you need to call SPWeb.Update() in order to persist the changes to the underlying database and AllProperties is no different, so don’t forget to do so.

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

Links

SPWeb.AllProperties Property
Get and set property bag values in SharePoint 2013 apps using CSOM

Last edited Mar 31, 2014 at 6:42 AM by dvd73, version 10

Comments

mattgl Nov 11, 2014 at 1:35 PM 
Please change the example. People might be led to the assumption that it makes sense to save an SPWeb object to the property bag. This is definitely not recommended, since the underlying SPRequest object would also be added to the property bag.