Sharepoint 2010 and SPWeb.AllProperties / SPWeb.Properties

15 March 2011

You're right - it's confusing. There's usually only one reason in SharePoint why things end up with totally non-common-sense names (see SPSite/SPWeb), and that reason is backwards compatibility. SPWeb.AllProperties is the recommended property bag for a SPWeb object, while SPWeb.Properties is maintained for the benefit legacy code. What's the difference between the two?

SPWeb.Properties is a StringDictionary, and doesn't support casing for keys/values (everything gets converted to lowercase), while SPWeb.AllProperties is a Hashtable, and supports a few other datatypes apart from strings (I believe ints and DateTimes are supported)

Instead of working directly with the SPWeb.Properties StringDictionary, SharePoint 2010 now provides four methods to manage properties:

  • SPWeb.GetProperty(Object key)
  • SPWeb.AddProperty(Object key, Object value)
  • SPWeb.SetProperty(Object key, Object value)
  • SPWeb.DeleteProperty(Object key)

Don't let the "object" method signature fool you; I did a quick test with adding a boolean value to SPWeb.AllProperties, but when it was later retrieved, it came back as a String instead of a boolean. Not too big of an issue, but 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.

Tags: properties, SharePoint 2010

Add a Comment

No Comments