Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to grab a key/value pair from a specific property bag but can't find any documentation or example code on how to grab a property bag. Currently I can only seem to grab allProperties. Here is my code:

    <CommandUIHandler
          Command="FCS.Intake.Tab.Reports.TL"
          CommandAction="javascript:
          function getWebProperty() {

        var ctx = new SP.ClientContext.get_current();
        var web = ctx.get_site().get_rootWeb();
        this.props =  web.get_allProperties();

        ctx.load(web);

        ctx.executeQueryAsync(Function.createDelegate(this, gotProperty), Function.createDelegate(this, failedGettingProperty));
    }

    function gotProperty() {
      window.location.href = this.props.get_item('PropertyKey1');       
    }

    function failedGettingProperty() {
        alert('failed');
    }
  getWebProperty();"
        />

This doesn't work because the key/value pair is located in a property bag as opposed to a site property.

Does anyone know how to grab a specific property bag using ECMA script?

share|improve this question
add comment

3 Answers

Actually you can grab a specific property as mentioned here.

The trick is to load, not the web but the object returns from "web.get_allProperties".

Then, you just need to call .get_item('name_of_the_prop_you_want_to_retrieve')

Hope it helps

share|improve this answer
add comment
up vote 1 down vote accepted

I was unable to find a way to grab a specific property bag, but using the information from Aaron's post (loading this.props), one can grab a property bag as an xml string, parse that xml and grab the specific value they are looking for. Here is how I did it:

function getWebProperty(propKey, propBagName) {          
  var ctx = new SP.ClientContext.get_current();
  var web = ctx.get_site().get_rootWeb();
  this.props =  web.get_allProperties();
  this.propKey = propKey;
  this.propBagName = propBagName;
  ctx.load(web);
  ctx.load(this.props);
  ctx.executeQueryAsync(Function.createDelegate(this, gotProperty), Function.createDelegate(this, failedGettingProperty));
}

function gotProperty() {
      var myPropBag = this.props;
      // Grab specific property bag as xml
      var myPropBagXML = myPropBag.get_fieldValues()[this.propBagName];        
      var parser = new DOMParser();
      var xmlDoc = parser.parseFromString(myPropBagXML, 'text/xml');
      xmlDoc.async = 'false';
      var propValue = xmlDoc.getElementsByTagName(this.propKey)[0].childNodes[0].nodeValue;
      alert(propValue);                
}

function failedGettingProperty() {
    alert('failed');
}
share|improve this answer
add comment

Try loading this.props

function getWebProperty() {

    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_site().get_rootWeb();
    this.props =  web.get_allProperties();

    ctx.load(web);
    ctx.load(this.props); //need to load the properties explicitly
    ctx.executeQueryAsync(Function.createDelegate(this, gotProperty), Function.createDelegate(this, failedGettingProperty));
}    

Then in your success function, you need to access the property bag's values by running it through get_fieldValues() and then an indexer for the key to the property.

function gotProperty() {
    var myPropBag = this.props;
    alert(myPropBag.get_fieldValues()["allowdesigner"]); //returns the value of the key allowdesigner
}

Firefox's firebug will allow you to do

console.log(myPropBag.get_fieldValues());

which will print out all the property bag keys to the console window.

Thanks, -@SharePointAP

share|improve this answer
    
This is great, thanks. Is there any documentation on SP2010 JavaScript client object model? I couldn't find much. –  Meyer Denney May 31 '12 at 19:46
    
Also I know which property bag I want to grab, I still don't know how to grab it. I have tried: var myPropBag = this.props; var myPropBag1 = myPropBag.get_fieldValues()['propertyBag']; alert(myPropBag1.get_fieldValues()['myPropBagKey']); With no luck. –  Meyer Denney Jun 1 '12 at 17:16
1  
The documentation on the Javascript client object model isn't as extensive as the server object model. But you could start here: msdn.microsoft.com/en-us/library/ee538253.aspx –  Aaron Jun 1 '12 at 18:18
    
There's just one property bag there so you can simply do alert(myPropBag.get_fieldValues()['myPropBagKey']); Just make sure you're loading this.props - ctx.load(this.props) before executing your success function. This will ensure the property bag is loaded. –  Aaron Jun 1 '12 at 18:20
1  
I only know of a few property bags: Farm, WebApp, SiteCollection, Web and List. If there are more property bags than that, then that's outside my expertise, sorry. But in the code above, we're getting the web's allProperties. It should just be a hashtable of key/val pairs. If the key returns xml, that may be the value and something you have to parse out. –  Aaron Jun 1 '12 at 19:05
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.