Now that the SharepointBar is open, we have to make our best to find time and write some article to share with interesting quality. I finally found a subject about I found no real information about on the web : how to manipulate the property bag of a site (or anything else) in Sharepoint 2010 with ECMA script ?

That was one of our challenge with my colleague (@ypeneveyre) in one of our latest project. Server side code was of course prohibited (as often with our clients) and we needed to store some key/values on sites.

I started my researches and quickly found help on this site, which was great, but I faced a very strange issue : if I refresh the page and try to reload the stored value, I didn’t receive any value. So I decided to writes two separated functions :

- one to write a given property (key and value in parameters)
- one to read  (key in parameter)

Write function

The job was already done actually with the code of James Love.


function writeProperty(propertyName, propertyValue) {
var web = clientContext.get_site().get_rootWeb();
this.props = web.get_allProperties();
this.props.set_item(propertyName, propertyValue);
//the next line is very very important !
web.update();
clientContext.executeQueryAsync(Function.createDelegate(this, writePropertySuccess), Function.createDelegate(this, writePropertyFailed));
}
function writePropertySuccess(){
var nid = SP.UI.Notify.addNotification("Property registered", false);
}
function writePropertyFailed(sender, args){
var nid = SP.UI.Notify.addNotification("Failure while trying to write the property: " + args.get_message(), false);
}

I just needed to check that the value and the key were really stored and registered in my site. There are several ways to check that (for sites):

- SharePoint Designer

  • Open SharePoint Designer, connect to the site you want to register keys and values in.
    SharePoint Designer Site Option
  • Locate the site options button and check the parameters tab. Tadaaaaaaa, done !
    SharePoint Designer Parameters Tab

- SharePoint Manager

  • Download SharePoint Manager, free tool available on Codeplex
  • You need to be remotely connected on one of your front end server AND you must run this tool as administrator (I know, if you are an external consultant, it’s sometimes difficult to have so much power, but as consultant you must also practice your political and good looking abilities).
  • Unzip SharePoint manager in a temp folder and right click, run as administrator. Let’s go !
  • Locate the web site on which you tried to save properties and open the tree, there is a “Property bag” node, open it and check your property, the value will be displayed on the right panel
    SharePoint Manager Property Bag

Read function

That was my really problem actually because my key and value was well stored in my site. But it was really really really easy.

function readPropertyParam() {
readProperty($("#propName").val());
}
function readProperty(propertyName) {
var web = clientContext.get_site().get_rootWeb();
this.props = web.get_allProperties();
clientContext.load(this.props);
clientContext.executeQueryAsync(Function.createDelegate(this, readPropertySuccess), Function.createDelegate(this, readPropertyFailed));
}
function readPropertySuccess() {
var nid = SP.UI.Notify.addNotification(this.props.get_item(propertyName), false);
}
function readPropertyFailed(sender, args) {
var nid = SP.UI.Notify.addNotification("Failure while trying to read the property: " + args.get_message(), false);
}

See, I told you it was easy !

Be Sociable, Share!
  1. Suresh Pydi says:

    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