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

About bentedder

Ben Tedder is a front-end web developer. He loves WordPress, SharePoint, and Drupal, along with jQuery, CSS, and good 'ol HTML. +Ben Tedder on Google or follow @bentedder

Comments

I try to respond personally, but things get busy sometimes :)
  1. Dennis says:

    Nice post. I found this very useful for my project. But my next step is how to do pagination? I don’t want to get the entire result list returned and I can not use the camlquery for the returned list.

  2. Daniel says:

    Hi

    Nice post. I think I will modify if to pickout lists that have certain content type.

    BTW what is the best place to put this jquery: in a CEWP or the whole syntax under

    Incidentally, I did try putting the jQuery functions in an external .js file which I load from the master page. However, this did not seem to work.

    Daniel

  3. Daniel says:

    Hmmmmm . The missing place holder which does not appear above is:

    asp:Content ContentPlaceHolderId=”PlaceHolderMain” runat=”server”

    • bentedder says:

      Yeah, you should be able to put these scripts in an external .js file, and then reference it in a page layout, page, or even master page. I put them in the page (using a blank .aspx page in a page library or something).

      • Daniel W says:

        Hi

        Just see your reply. Thanks. I have a related thought. I want to rollup task data for a user that spans multiple sites in a site collection.

        I could do this serverside via Visual Studio

        //Query all the SPWebs in this SPSite
        query.Webs = "";
        using (SPWeb web = site.OpenWeb())
        DataTable resultsTable = web.GetSiteData(query);

        Alternatively, I could use your client side approach. Hence when would I want to use one over the other. Note, I am replacing a CQWP so I have a lot of xlst functions that would be nice to resue.

        • bentedder says:

          Yeah, I think client-side is where it’s heading. But then again I’m more of a designer than a developer, so that works in my favor!

          Yeah, I’ve used SPServices to roll up data from a task list, and it wouldn’t be that difficult to load it up from multiple lists (for instance, all named “tasks”). Regarding the XSLT though…that’s something I haven’t used much with anything other than CQWP’s. But it shouldn’t be that much of a headache to just copy your xslt template over and strip out all the xslt stuff, leaving just the styles and placeholders. From there you can put in all the data you get back from SPServices.

          For me, doing this kind of thing is easy with SPServices because I can really control every aspect of how my page looks and operates.

  4. James says:

    Ben,
    Thank you for sharing your SharePoint knowledge.

    I am working on an intranet where I’m trying to roll-up multiple lists (right now various blog and announcement lists exist) into one for the homepage (a news ticker of sorts). This code seems to be what I’ve been looking for, however, I have trouble getting it to work.

    I can see the posting goes through properly and it returns the XML I need, but it won’t display the data at all. I have added a ul with a specific ID, followed your example up top, yet still, nothing parses itself into the unordered list I’ve specified. Basically it remains blank. I know its probably impossible to troubleshoot with me, but in the event you have an idea what it might be.. I thought I would try. Thanks !

    • bentedder says:

      Happy to help. Can you send over your code? or a screenshot?

    • While rolling things up across multiple lists is certainly possible, and sometimes even preferable, with SPServices, in other cases it makes more sense to use a DVWP with DataSourceMode=”CrossList”. This means that the gathering of the content occurs server-side which can lead to a better user experience than if the list iteration happens on the client.

      As with all things, it depends.

      M.

      • James says:

        Hi Guys,

        Better late than never for an update on this. Thank you Marc for your input, it put me in the right direction. Using a DVWP was the solution!

        It came out looking great with a bit of jquery scrolling.

        I’m going to send over a screenshot to both of you. Cheers!

Join the discussion!