SPServices and jQuery: Rollup from multiple lists and put the results in a new list

I just created a post about rolling up list items from multiple subsites within a site collection. In that post we took all of the titles of all the items in all the “Pages” lists in all of the subsites of the site collection. In this post we’ll add to that code, and send that resultset into an empty list (already created). We’ll cover creating the new list using SPServices and jQuery in a different post.

Here’s the order of how it happens:

  1. GetAllSubWebCollection (pull all sub-sites of the site collection)
  2. GetListCollection (pull all lists from each sub-site)
  3. GetListItems (pull all items from each “Pages” list)
  4. UpdateListItems (create new records for each item found and put them in another list)

I’m going to let the code speak for itself. As usual, check out SPServices and Mark Anderson’s blog.

$().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 title = $(this).attr("ows_Title");
	       $().SPServices({
		operation: "UpdateListItems",
		async:false,
		batchCmd: "New",
		listName: "My new list",
		valuepairs: [["Title", title]],
		completefunc: function(xData, Status) {
		
                }
	 })

      });
     }
    });
   }
  })
 }
   });
  })
 }
})

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. Venky says:

    Is there any way I can filter only to get latest SPLIST item version when I am using to get all BaseType=0 (custom lists).

    Thanks in advance.
    Venky

    • bentedder says:

      Venky,
      Are you trying to get just the latest version of a single item, or the latest version of all items from a list? Can you send over the code you have so far?
      Ben

  2. Shawn says:

    Not sure who is the main target user, but normal user would not able to access this function ‘GetAllSubWebCollection’, I was stuck there.

Join the discussion!