5

I have simple JSOM script to get all the list and it's default view url but i am not getting default view url property

var context; 
var lists;

// This code runs when the DOM is ready and creates a context object which is needed  // to use the SharePoint object model $(document).ready(function () {

    context = SP.ClientContext.get_current();
    lists = context.get_web().get_lists();
    context.load(lists);
    context.executeQueryAsync(function () { reqSuccess(); }, function () { reqFail(); });
    });

function reqSuccess() {
    var listEnumerator = lists.getEnumerator();
    while (listEnumerator.moveNext()) {
        var list = listEnumerator.get_current();
        var listTitle = list.get_title();
        var listUrl = list.get_defaultViewUrl(); //Not getting this property
    } }

function reqFail(sender, args) {
    alert(args.get_message()); }
6

The syntax for loading Default View URL is below.

context.load(lists,'Include(DefaultViewUrl)');

Default view is a valid list property and we need to explicitly load it.

  • 1
    you are right just but the syntax is little different context.load(lists, "Include(Title, DefaultViewUrl)"); – SBP Oct 22 '14 at 14:04
  • Thansk @SandipPatil, I have updated my answer. – Amal Hashim Oct 22 '14 at 15:24
4

Since List.DefaultViewUrl property is not loaded by default you have to include it explicitly as demonstrated below:

var context = SP.ClientContext.get_current();
var lists = context.get_web().get_lists();
context.load(lists,"Include(Title, DefaultViewUrl)");
context.executeQueryAsync(
  function () { 
    var e = lists.getEnumerator();
    while (e.moveNext()) {
        var list = e.get_current();
        var listTitle = list.get_title();
        var listUrl = list.get_defaultViewUrl(); 
        //...
    }
  },logError);


function logError(sender,args){
    console.log(args.get_message());
}

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.