3

I try to dynamically get the names of all the lists in my site using a content query web part, but apparently a CQWB can only retrieve the items in the list, but not the list name? I'm wondering how can i go this, maybe using jquery? Thank you very much for your help!!!

3 Answers 3

4

+1 @james

Here's the documentation to the SPServices library. http://spservices.codeplex.com/wikipage?title=Lists&referringTitle=%24%28%29.SPServices

There isn't an example on there, but I'll post one here:

    $().SPServices({ 
        operation: "GetListCollection", 
        completefunc: function( xData, Status ) {
            console.log( Status );
            console.log( xData.responseText );

            $( xData.responseXML ).find("Lists > List").each(function() {
                var $node = $(this);

                console.log( $node.attr("Title") );
            });
        } 
});

Hope this helps!

1
3

Another option is to use the JavaScript Client Object Model:

ExecuteOrDelayUntilScriptLoaded(function(){

    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var lists = web.get_lists();
    clientContext.load(lists);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onListsQuerySucceeded), Function.createDelegate(this, this.onListsQueryFailed));

    function onListsQuerySucceeded() {
        var listEnumerator = lists.getEnumerator();
        while (listEnumerator.moveNext()) {
            var oList = listEnumerator.get_current();
            //Do something with oList.
        }
    }  

    function onListsQueryFailed(sender,args) {
        alert("Failed: "+ args.get_message());      }
},"sp.js");
2

GetListCollection in Lists.asmx will get this for you.

http://msdn.microsoft.com/en-us/library/websvclists.lists.getlistcollection.aspx

Not yet sure if SPServices has implemented this, if not - give @sympmarc a shout on Twitter and suggest it to him :)

4
  • SPServices currently does not have a wrapper for GetListCollection according to the documentation. Aug 1, 2012 at 18:21
  • Just checked - spservices.codeplex.com/… - apparently it does? I'll let Marc add more info.
    – James Love
    Aug 1, 2012 at 18:22
  • herpaderp, there just aren't any example pages of it being used. Aug 1, 2012 at 18:24
  • 2
    GetListCollection has been there since way back in v0.2.3. Forever! Aug 1, 2012 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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