The perils of storing SPWeb metadata
On Friday while searching for how to store metadata in a web object, I was looking through the MSDN documentation for the SPWeb class. The SPWeb.Properties property looked like just what I needed. As I was reading the detailed description, I found this gem:
RemarksThis property returns only a subset of the metadata for a Web site. To get all the metadata, use the AllProperties property.
Huh. So why does it return only a subset? What subset does it return, a random selection perhaps? If it only returns a subset, shouldn’t they have named it SomeProperties or perhaps NotAllProperties instead?
The MSDN docs din’t explain anything, but Google always knows the answer. It turns out that the Properties property (besides having a confusing name) represents an older mechanism for storing metadata. It has been replaced by the newer AllProperties property, but is still present for backwards compatibility.
Properties returns a SPPropertyBag object, a custom type which can store key/value pairs — keys are case insensitive and are converted to lowercase when added. AllProperties on the other hand uses a standard Hashtable collection to store its key/value pairs and has case sensitive keys. Furthermore, the SPPropertyBag class has its own Update method which updates the database with any changes to the collection. The AllProperties collection on the other hand is saved by calling the Update method of the SPWeb object.
To add to the confusion, keys added to Properties are also stored in AllProperties, but not the other way around. This explains the “returns only a subset of the metadata” part of the property description.
But it doesn’t stop there — no, joy of joys, there’s more! In SharePoint 2010, four new methods were added to the SPWeb class.
Thanks, that’s so very helpful. Four new methods that deal with SPWeb metadata, and no description or explanation. Which collection do they store their data in? Properties? AllProperties? Both? Their own, separate collection?
Thankfully PowerShell, which I’m just learning to use, allowed me to quickly test the behaviour of the new methods. Perhaps not unexpectedly, they read and write to AllProperties, the more recent of the metadata collections.
It seems like all this has the potential to cause great confusion if not all developers on a team use the same collection to store their metadata. Especially the way Properties updates AllProperties but not vice versa, and the fact that the new methods are not documented, nor is the distinction between Properties and AllProperties explained properly. Luckily though, as I have just recently started to work with SharePoint, this issue hasn’t had the chance to cause me any grief yet… on the contrary, the process discovering all of this on a Friday afternoon proved to be a great source of entertainment.

