CSC510240. Do not use SPWeb.Properties collection.
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).
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
$tempWeb = $web.GetProperty("SPWebObjectKey")
This outputs to http://sharepoint
Now that we have used our property and no longer need it, it’s time to delete it:
Get and set property bag values in SharePoint 2013 apps using CSOM