10

We have requirement to get all the non hidden lists of current web using JavaScript Client Object Model(JSOM). I have tried the below code but failed to get the desired result.

function GetListDetails() {
    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_web();
    var user = web.get_currentUser();
    user.retrieve();
    var list = web.get_lists();
    ctx.load(web);
    ctx.executeQueryAsync(
        function () {
            tddetails.innerHTML = user.get_loginName();
        },
        function (data) {
            alert('failed to get list. Error:' + data.get_message());
        });

    return false;
}

Looking for any sort of help.

  • you are not getting the hidden lists over here? Check if you are running the code as a administrator in that site? – suresh Jan 11 '12 at 13:19
7

You should use SP.ListCollection.getEnumerator() method in order to enumerate lists, and SP.List.hidden property to check, whether the list is hidden or not.

The corresponding code will look something like this:

function GetHiddenLists() {
    var ctx = new SP.ClientContext.get_current();
    var web = ctx.get_web();
    var lists = web.get_lists();
    ctx.load(lists);
    ctx.executeQueryAsync(
        function () {
           var listEnumerator = lists.getEnumerator();
           var listInfo = '';

           while (listEnumerator.moveNext()) {
             var oList = listEnumerator.get_current();
             listInfo += 'Title: ' + oList.get_title() + ' Hidden: ' + oList.get_hidden().toString() + '\n';
           }
           alert(listInfo);
        },
        function (sender, args) {
            alert('failed to get list. Error:' + args.get_message());
        });

    return false;
}
  • Thanks omlin, That was really useful. Can there be any CAMLQuery stuff to get the required lists only(Non hidden) – suryakiran Jan 11 '12 at 13:33
  • @suryakiran unless you have more than 1000 lists in your site, the performance hit will be negligeable, so I think this approach will be ok. And anyway, there is no SPSiteDataQuery analogue for Client Object Model: sharepoint.stackexchange.com/questions/7206/… – Andrey Markeev Jan 11 '12 at 13:38

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.