Omicron Llama

Coding all day, every day.

Making use of the Property Bag in the ECMAScript Client Object Model

This article is a cross-post of the originally published article from NothingButSharePoint.com, available here: https://www.nothingbutsharepoint.com/sites/devwiki/articles/Pages/Making-use-of-the-Property-Bag-in-the-ECMAScript-Client-Object-Model.aspx

The “Property Bag” is a really quick and easy way to store key/value pairs that can be associated to a particular web site. You can use this to easily store settings for an application that you might want to scope to a single Website, or even an entire Site Collection (by referring to the RootWeb of the current Site Collection).

We can make use of this in the Javascript/ECMAScript Client Object Model of SharePoint, using the allProperties property of the web object.

Here is a quick example of both setting and getting a value from the property bag, using Javascript.

When we call getWebProperty, the usual stuff of setting up the Client Context is done to get a handle on the current site. I’m also getting the RootWeb of the current site collection, so this property could be retrieved in any site within the site collection. By creating a variable called ‘props’ and setting its scope to ‘this’, I can retrieve it in my success callback.

As you can see, it’s simply a case of calling ‘set_item’, passing in the name of a property and the value to set, and also calling ‘get_item’, passing the name of a property, to retrieve that value.

The main difference between this and the server object model is that because we use traditional ‘getter’ and ‘setter’ methods (rather than Property accessors) we don’t need to trap for possible ArgumentOutOfRange exceptions in our code. If the value doesn’t exist, the method call to ‘get_item’ will return null. In this sense, to me, this makes handling the Property bag far simpler, with less code needed to write to and get from the property bag.

One thought on “Making use of the Property Bag in the ECMAScript Client Object Model

Leave a Reply

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