2

How can I get title and url using allproperties? I can get custom proeprty but do not know how to get title and url.

I am not able to know as why I get the alert twice in the following code,

function onGetSubwebsSuccess(sender, args) {
        var webEnumerator = webCollection.getEnumerator();

        while (webEnumerator.moveNext()) {
            var web = webEnumerator.get_current();
            this.props = web.get_allProperties();
            context.load(this.props);
            context.executeQueryAsync(Function.createDelegate(this, gotProperty), Function.createDelegate(this, failedGettingProperty));
        }
    }

    function gotProperty() {
        var myPropBag = this.props;
        alert(myPropBag.get_fieldValues()["AllowUser"]); 

and how can I send web into the gotProperty method.

Please donot answer what i already know and understand the question Please

I know how to get title and url but look, I want to get web's title, url and a custom property bag and then add them to a html table, If that makes me more understandable ? Thank You

I am getting sub webs and then each sub webs property bag, now because its a Async query enumerator isnot waiting for query to complete and moves to next web. So I am not able to match which web has what title url and there custom property.

  • 1
    Your question is very clear in that you want to get title and URL from property bag, I think that is the reason why people are telling you how you should get this info (ergo not from the property bag). If you are asking something else you should try to clarify what – Robert Lindgren Apr 12 '14 at 18:33
  • thank you for advise; but question title and my code is explaining what i am wanting ! – Mathematics Apr 12 '14 at 18:36
  • 2
    As a non-native speaker of English, if a bunch of smart people all misunderstand what you are asking you might consider your question is poorly worded. – Robert Kaucher Apr 13 '14 at 1:45
  • I've revised my answer, load the web properties in the while and then use a single executeQueryAsync outside of the while to load the webs at once. – Robert Kaucher Apr 13 '14 at 2:19
4

In order to get value from Web.AllProperties property (PropertyValues type) you could use the following ways:

Example: get Web AllProperties

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var props = web.get_allProperties();
context.load(props);    
context.executeQueryAsync(function(){
     console.log(props.get_item('SRCH_ENH_FTR_URL'));  
     console.log(props.get_fieldValues()['SRCH_ENH_FTR_URL']);
  },
  function(sender,args){
     console.log(args.get_message());
  }
);

Example: get Web properties (Title, Url)

var context = new SP.ClientContext.get_current();
var web = context.get_web();
context.load(web);    
context.executeQueryAsync(function(){
     console.log(web.get_title());  
     console.log(web.get_url());
  },
  function(sender,args){
     console.log(args.get_message());
  }
);

In order to access Web object in your handler you could write:

function gotProperty() {

   var web = props.get_context().get_web();
   //...
} 

In order to make current web accessible in your handler add the line

context.load(this.web);

How to load and save properties for sub webs

The below example demonstrates how to load properties from sub webs.

Note: Web properties be requested using a single query, it means you don't need to perform any subsequent requests using executeQueryAsync. Web properties are stored in array websProps

(function(){
  var ctx = SP.ClientContext.get_current();
  var webs = ctx.get_web().get_webs();
  var websProps = {};
  ctx.load(webs,'Include(Url,AllProperties)');  //Note: Explicitly specify which properties to load for Web  
  ctx.executeQueryAsync(function(){
       var e = webs.getEnumerator();
       while (e.moveNext()) {
          var web = e.get_current();
          var webUrl = web.get_url();
          var props = web.get_allProperties();
          var propNameValues = props.get_fieldValues();
          //save properties  
          //Note: For demonstration purposes vti_defaultlanguage property is saved, change it to your property name
          websProps[webUrl] = {'DefaultLanguage': propNameValues['vti_defaultlanguage']};
       }
       console.log(JSON.stringify(websProps)); 
    },
    function(sender, args){
        //Handle error
    }
  );
})();
  • 1
    the answer has been updated. FYI, title and url of Web object are NOT stored in AllProperties, they are exposed via Web object. – Vadim Gremyachev Apr 12 '14 at 16:31
  • the answer has been updated again. See the section - How to load and save properties for sub webs. It demonstrates how to load web properties using the single request – Vadim Gremyachev Apr 13 '14 at 15:03
  • You are welcome, glad this helped you! :) – Vadim Gremyachev Apr 20 '14 at 14:04
2

Now that you have explained your issue better and I see your question actually has nothing to do with getting the title and URL, I can make a suggestion. Load the web properties in a batch. Then use a single execute query after the while. Anonymous functions embedded just to keep lines of code down.

var ctx = SP.ClientContext.get_current();
var webCollection = ctx.get_web().get_webs();
var webs = [];
ctx.load(webCollection);
ctx.executeQueryAsync(function(sender, args){
    var webEnumerator = webCollection.getEnumerator();

    while (webEnumerator.moveNext()) {
        var web = webEnumerator.get_current();
        ctx.load( web.propertyBag = web.get_allProperties() );
        webs.push(web);
    }
    ctx.executeQueryAsync(function(sender,args){
        //The items in web collection now have properties.
        //Do stuff here.
        console.log("Do stuff here.");
      },function(sender,args){
        //Handle batchload errors here.
      });


    },function(sender, args){
        //Handle error
});
  • Thank You, but how I will know which property of which subweb. Thanks Agin – Mathematics Apr 13 '14 at 7:38
  • Sorry, it was late. I forgot the essential part. web.propertyBag = web.get_allProperties(); Then just access the title and URL as usual. – Robert Kaucher Apr 13 '14 at 23:14
  • Updated the code. – Robert Kaucher Apr 13 '14 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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