4

I am trying to get the default view url of a list in SP hosted app. Below is the code i am trying, on execute success method.

spListobj.get_defaultViewUrl();

It is returning me this error : The property or field 'DefaultViewUrl' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

Then ss, the error suggests i include the DefaultViewUrl property while loading the List object as:

ctx.load(spListobjCollection, 'Include(Title,BaseTemplate,DefaultViewUrl,ItemCount)');

Edit: I am loading the listCollection together and using enumerator to get individual list . But still no luck.

4

The proper syntax for loading SP.List.defaultViewUrl Property:

context.load(list,'DefaultViewUrl');

Working example

var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listTitle);   
context.load(list,'DefaultViewUrl');   
context.executeQueryAsync(
   function(){
      console.log(list.get_defaultViewUrl());    
   }, 
   function(sender,args){
      console.log(args.get_message());    
   }
);

Update

Since DefaultViewUrl property is available for a List object, you have to specify explicitly to load this property:

context.load(list,'DefaultViewUrl'); 

The line:

ctx.load(listItems, 'Include(DefaultViewUrl)');

is incorrect since it tells to load DefaultViewUrl property from ListItem object, which in turn does not contain such a property.

Example 2

var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listTitle);   
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());

context.load(items);   
context.load(list,'DefaultViewUrl');  //explicitly specify to load  DefaultViewUrl property of List object
context.executeQueryAsync(
   function(){
      for(var i = 0; i < items.get_count();i++) {
         var item = items.getItemAtIndex(i);
         console.log(item.get_fieldValues());
      }   
      console.log(list.get_defaultViewUrl());    
   }, 
   function(sender,args){
      console.log(args.get_message());    
   }
);
  • thanks for your reply. my bad i copied wrong code as i am, loading list collection together and then using enumerator to get each list. – Unnie Jul 4 '14 at 9:59
  • The answer has been updated – Vadim Gremyachev Jul 4 '14 at 10:30
  • Basically, specifying ctx.load(listItemsCollection, 'Include(DefaultViewUrl)') is incorrect since it tells to load DefaultViewUrl property from ListItem object, which in turn does not contain such a property – Vadim Gremyachev Jul 4 '14 at 10:33
  • i am loading list collection not listitem collection – Unnie Jul 4 '14 at 16:44

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.