3 10 2011
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.
1 |
function getWebProperty() {<br> var ctx = new SP.ClientContext.get_current();<br> var web = ctx.get_site().get_rootWeb();<br> this.props = web.get_allProperties();<br> this.props.set_item(“aProperty”, “aValue”);<br> ctx.load(web);<br><br> ctx.executeQueryAsync(Function.createDelegate(this, gotProperty), Function.createDelegate(this, failedGettingProperty));<br>}<br><br>function gotProperty() {<br> alert(this.props.get_item(“aProperty”));<br>}<br><br>function failedGettingProperty() {<br> alert("failed");<br>}<br> |
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.
Programmatically add Ribbon Custom Action to an Existing List Video – SharePoint Branding with CSS Customisations
Nice post. Here is one more post explaining to get and set property bag values in SharePoint 2013 apps using CSOM
http://sureshpydi.blogspot.in/2013/05/set-and-get-property-bag-values-in.html
[…] You can make use of property bag of web object. It stores properties as key value pairs. Check this for reference: omicron-llama.co.uk/2011/10/03/making-use-of-the-property-bag-in-the-ecmascript-client-object-model/ […]