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()); }

2 Answers 2

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.

2
  • 1
    you are right just but the syntax is little different context.load(lists, "Include(Title, DefaultViewUrl)"); Commented Oct 22, 2014 at 14:04
  • Thansk @SandipPatil, I have updated my answer. Commented Oct 22, 2014 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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.