ben tedder : code things

SPServices and jQuery: pull items from multiple lists in SharePoint 2010

Let's say you want to display all of the titles of every page library in your site collection. Using jQuery and SPServices, it's a breeze it's definitely possible.

What we have here are three calls of the SPServices function. First, we call the GetAllSubWebCollection *operation. This will bring us all sites (webs) within a certain site collection. Once we have that, we'll then iterate through each web to find all lists (2nd SPServices call) using the *GetListCollection *function. Once we have all lists, we'll iterate through each list to look for any lists with the title "Pages". This will give us every page library of every site in our site collection. Then we we will call our 3rd and final SPServices function, *GetListItems to bring us all of the titles of all the items (in every list, in every site of this site collection). Finally, for our last act, we'll then link each title of each item of each list of each subsite of the site collection to it's specific URL.

But then we have to do something with that chunk of data. The last line in our code is a simple jQuery function that appends an LI with each title inside an empty UL tag with the ID of #sitelist (somewhere in your page).

Phew!

…ok, here's the code (I just tested it, it works!):

$().SPServices({
    operation: "GetAllSubWebCollection",
    webURL: "/",
    async:false,
    completefunc: function(xData, Status) {
        $(xData.responseXML).find("Web").each(function(){
$().SPServices({
 operation: "GetListCollection",
 webURL: $(this).attr("WebFullUrl"),
 async:false,
 completefunc: function(xData, Status) {
  $(xData.responseXML).find("List").each(function(){
  //function to get all items inside every list called "Pages"
  if($(this).attr("Title") == "Pages") {
   $().SPServices({
    operation: "GetListItems",
    webURL: $(this).attr("WebFullUrl"),
    async: false,
    listName: $(this).attr("Title"),
    CAMLViewFields: "<ViewFields><FieldRef Name='EncodedAbsUrl' /><FieldRef Name='Title' /></ViewFields>",
     completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
       var url = $(this).attr("ows_EncodedAbsUrl") + "";
       var liHtml = "<li><a href='" + url + "'>" + $(this).attr("ows_Title") + "</a></li>";
       $("#sitelist").append(liHtml);
      });
     }
    });
   }
  })
 }
});
        })
    }
})

As usual, thanks to Marc Anderson and his SPServices library