0

In JSOM, we can not access a property of an object before load and excecute. But I don't understand why code 1 works and code 2 doesn't:

Code 1: Here I am accessing the List property of web

var web= context.web;
var lists=web.get_lists().getByTitle("myList"); //this works. 

Code 2: Here I am accessing the Title property of web

var web=context.web;
var title=web.get_title(); //this throw exception

Both the code accessing the web properties but one works and other doesn't. Am I missing something?

UPDATE: made code changes for JSOM.

3

In Code 1 you have only client side references for server side objects (both the web and lists variables, the later one is a single list, should be called list), but you don't read any properties of them. If you would try to get the title of the list (via the get_title() method), you would receive an exception as well, as this property was not yet requested from the server.

The same is true for Code 2. In the first line you have no exception, as you ask only for a reference. In the second line you try to access a property, that you have not yet requested, so an exception is thrown.

| improve this answer | |
  • So it means when ever I am asking for a reference it wont throw a exception but if I try to read the properties then I will get a exception. – nbi Feb 19 '15 at 7:28
2

In your first example with SP.ListCollection.getByTitle method you are only getting non-initialized List client object. In order to retrieve List object properties, the request has to be submitted to the server via SP.ClientContext.executeQueryAsync method as demonstrated below:

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle(listTitle);
ctx.load(list);
ctx.executeQueryAsync(
      function() {
         console.log(list.get_title());
      },
      function(sender,args){
         console.log(args.get_message());
      }
    ); 

In the second example the situation is similar, before getting client object properties it has to be requested via SP.ClientContext.executeQueryAsync method. In order to access Web.Title property use the following code:

var listTitle = list.get_title();   
| improve this answer | |
0

SP.Web does not have property "Title". You have to use method get_title() for retrieving it. See the link

var value = SP.Web.get_title();
| improve this answer | |
  • Updated the code – nbi Feb 19 '15 at 7:37

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.