Down and Dirty SPServices Solution to Solve an Emergency Request

I got one of those “red alert”, “all hands on deck” emails this morning. Someone was trying to find all of the Document Libraries in a Site Collection with a certain name that had at least one item. They were trying to solve some specific client problem (I know not what!) and it was a numero uno emergency.

It would seem that this would be a simple question, but there’s no easy way to do this in SharePoint out of the box. They thought of the Content Query Web Part but weren’t sure if it would help them. I wasn’t sure, either.

Being the Middle Tier guy that I am, I thought of two potential ways to get a solution: a DVWP with DataSourceMode=”CrossList” or SPServices. They had one more rule: no SharePoint Designer, so the former was out. SPServices seemed like a perfect way to get a fast answer.

The basic solution is pretty simple:

  • Call GetAllSubWebCollection to find all of the sites in the Site Collection
  • For each site, call GetListCollection
  • For each list in the site, check to see if it is the right list, and if the ItemCount is non-zero, pop up an alert

At least this was a good start to solving the problem. I tossed together the script below in about 20 minutes (I forgot some of the syntax and had to read the docs!). In this example, I look for lists where Title=”Announcements”.

<script language="javascript" type="text/javascript" src="jQuery%20Libraries/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript" src="jQuery%20Libraries/jquery.SPServices-0.5.6.min.js"></script>
<script language="javascript" type="text/javascript">

	$(document).ready(function() {
		$().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") == "Announcements" && $(this).attr("ItemCount") != "0") {
									alert("Site:" + thisUrl + " List:" + $(this).attr("Title") + " ItemCount:" + $(this).attr("ItemCount"));
								}
							});
						}
					});
				});
			}
		});
	});

</script>

Just dropping this script into a Content Editor Web Part somewhere would let them know where these pesky lists were. No fuss, no muss, no server-side code. Sure, it’s not pretty, but it’ll get the job done.  The point is that SPServices and jQuery can be used for little quick stuff like this pretty easily when you need a fast answer.

Similar Posts

7 Comments

  1. I actually did something similar to this last week so it’s funny to see you post it this morning. I needed to build a nav list that had an entry to each folder inside a list called “Dashboards” across the entire collection.

    Like you, I started with GetAllSubWebCollection, then GetListCollection. It’s a shame that you can’t filter the GetListCollection results on the server vs. using a JavaScript if statement and touching every list.

  2. Did they give any reason for their “no SharePoint Designer” rule? Anyway, you can build a crosslist DVWP with a text editor, no need for SPD (cf. for example my user’s toolkit).

  3. Instead of doing this: if($(this).attr(“Title”) == “Announcements” would a CAML query work? I tried adding one, but it didn’t filter anything and I just got back all the results.

    Thanks
    Andrew.

Leave a Reply to Andrew Gaskell Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.