Tagged: spservices

Get Items from Multiple Lists using jQuery & SPServices

Update: Only works for Site collection administrators

I had a requirement to collate the newest Announcements from all the sub-sites and display them onto the front page of an intranet site. I found this solution from Ben Tedder which worked but it wasn’t giving me quite the right result. So I continue my search and found this solution from Marc D Anderson, which was giving the right results but within an alert.

get-pages   get-announcements

So I decided to try and combine these two solutions into one. It starts by looping through all the sites within the collection and sees if an Announcements list exist and if there are any items within it. The CAMLQuery filters by where Expires greater than or equal to today or Expires is blank, sorts by Created descending and limited to one item. Next it builds the url to the display form (DispForm.aspx), within a dialog box and displays the results to a bulleted list.

get-items-multiple-lists

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery.SPServices-2013.01.min.js"></script>
<script type="text/javascript" language="javascript">

$(document).ready(function() {

  var listName = "Announcements";

  $().SPServices({
    operation: "GetAllSubWebCollection",
    async: false,
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("Web").each(function() {
        var thisUrl = $(this).attr("Url");
        $().SPServices({
          operation: "GetListCollection",
          webURL: thisUrl,
          async: false,
          completefunc: function (xData, Status) {
            $(xData.responseXML).find("List").each(function() {
              if($(this).attr("Title") == listName && $(this).attr("ItemCount") != "0") {
                $().SPServices({
                  operation: "GetListItems",
                  webURL: thisUrl,
                  async: false,
                  listName: listName,
                  CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='EncodedAbsUrl' /><FieldRef Name='Title' /></ViewFields>",
                  CAMLQuery: "<Query><OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy><Where><Or><Geq><FieldRef Name='Expires' /><Value Type='DateTime'><Today /></Value></Geq><IsNull><FieldRef Name='Expires' /></IsNull></Or></Where></Query>",
                  CAMLRowLimit: 1,
                  completefunc: function (xData, Status) {
                    $(xData.responseXML).SPFilterNode("z:row").each(function() {
                      var url = $(this).attr("ows_EncodedAbsUrl");
					  var url = url.substr(0, url.lastIndexOf("/"));
					  var fullUrl = '"' + url + "/DispForm.aspx?ID=" + $(this).attr("ows_ID") + '"';
					  var liHtml = "<li><a style='cursor: pointer;' onclick='javascript:SP.UI.ModalDialog.OpenPopUpPage(" + fullUrl + ", null, 800, 600); return false;'>" + $(this).attr("ows_Title") + "</a></li>";
					  $("#listItems").append(liHtml);
                    });
                  }
                });
              }
            });
          }
        });
      });
    }
  });
});

</script>

<ul id="listItems"/>