SharePoint: The Wicked SPWeb.Properties PropertyBag

By | June 25, 2009

*** This article is a must read for any developer reading or writing Web (Site) properties ***

If you haven’t already noticed, there are two different API properties on the SPWeb class – AllProperties (a Hashtable) and Properties (a PropertyBag). Apparently the AllProperties is meant to replace Properties, but Properties was left in place for backwards compatibility. Here’s where things get wicked …

The unconventional PropertyBag data type stores its keys in all lowercase, thus not supporting case-sensitive keys, while the conventional Hashtable does support case-sensitive keys. On top of that, while entries added to Properties get propagated to AllProperties with a lowercase key, entries added to AllProperties do not get propagated to Properties. So this what Microsoft gives us developer peons to work with. This is why consultants like myself stay employed.

If you are working with property entries that only your custom application will be reading and writing from, simply always use AllProperties and you will be good to go. However I often find myself having to work with properties that are read or written by SharePoint internals, and SharePoint itself is very inconsistent in its interaction with Web (Site) properties, probably due to its evolving nature being a rather old product as far as software is concerned.

So the best solution that I have come up with to date is to add your entries to both API properties. This ensures the entry will be present in both collections, and also ensures the key will have the correct case in AllProperties. I have found though in order to add to both, the order of the update API calls is important – you must first update the SPWeb object followed by updating the SPWeb.Properties PropertyBag. Performing the updates in the reverse order prevents an entry with a case-sensitive key from being added to AllProperties, and only the lowercase-keyed entry will be exposed in AllProperties. Now that you are probably thoroughly confused, here is the code:

// Add a property entry
web.Properties[key] = value;
web.AllProperties[key] = value;
web.Update();
web.Properties.Update();
 
// Remove a property entry
web.AllProperties.Remove(key);
web.Properties[key] = null;
web.Update();
web.Properties.Update();

Take a look, or straight up copy, my SharePoint utility class that abstracts away Web property interactions.

11 thoughts on “SharePoint: The Wicked SPWeb.Properties PropertyBag

  1. mike hodnick

    I eagerly anticipate the introduction of the “SomeProperties” property on the SPWeb class in SharePoint 2010.

  2. joe

    I hear most properties are decreasing in value. MS never ceases to amaze with their endless inconsistent bullshit.

  3. Saumil

    How To Make these Perperties Searchabe…by sharepoint search…?

  4. Trent Post author

    @Mike I just confirmed there is no SomeProperties property in SP 2010, however, nothing has been done to clean up Properties and AllProperties.

  5. Trent Post author

    @Saumil You can’t. What are you storing here that you want to be searchable? Most likely you will want to store these values in a Custom List in order to make them searchable.

  6. Sreeju

    Thanks a lot… Actually i never noticed all properties before and this article gave me a sight…

  7. Russell Lindell

    Trent,
    Thanks for posting this. I got the following error (posting for future searches) and using the SPContext.Current.Web.Properties instead of SPContext.Current.Web.AllProperties was the root cause. It turned out when I reduced my privileges to Full instead of running as a farm admin in my development environment this became an issue.

    ///////////////////////////////
    Object reference not set to an instance of an object. at myNameSpace.myClass.get_MyPropertyNameValue()

    at myNameSpace.myClass..cctor()
    ///////////////////////////////

    BTW, its a small world to get help from a fellow BCA alumni. It looks like you are doing well.

    Russell ’01

  8. Carbon

    Thanks for the post. Saved me some hours of debugging.

  9. Andy Burns

    Thanks, that helped me too, though I found the order in which I updated the two properties collections seemed to be the issue for me.

  10. Sukri

    Thanks a lot. Check your Adsense! – a click as token of appreciation. Cheers.

  11. Gildat

    Thank You! It’s thank to you if consultants like me solve their impossible doubts.

Leave a Reply

Your email address will not be published. Required fields are marked *