Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

1 Answer 1

up vote 6 down vote accepted

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;
}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.