SPServices GetListItems Generator Demo Page

Someone asked a question in the SPServices Discussions this morning because they were getting an “Object not found” error. This type of error almost always means that the references to the .js files aren’t correct for your environment. I’ve done a few posts about how to remedy this, most notably Adding jQuery to a SharePoint Page: Step One, Always  and Adding jQuery+SPServices to a SharePoint Page: Step One, Always.

But that question wasn’t the cool part (I probably get at least one “Object not found” question a day”). The cool part was alfred_j_kwack‘s original post. In it, he provided a page you can use to generated SPServices calls to GetListItems just be making some selections on the page. It’s a great little tool for someone who is just getting started with SPServices and wants to see how the calls should be assembled. I probably saw it when he first posted it about a year ago, made a note to do something with it, and then got busy.

I made some tweaks to Alfred’s code to bring it in line with the current versions of jQuery and SPServices, with a little clean up as well, and posted it on my demo site as a new demo page called GetListItemsGenerator.

GetListItemsGenerator

The script uses WebUrlFromPageUrl, SiteDataGetListCollection, and GetList to make things work. Here’s the script, though it’s also available on the demo page.

$(document).ready(function() {

	waitMessage = "</pre>
<img style="display: block; margin-left: auto; margin-right: auto;" src="/_layouts/images/gears_an.gif" alt="" />
<pre>";

	$("form").submit(function(e) {e.preventDefault()});

	$('#inputURL').change(function() {
		//obtain the site's URL inputURL value
		$().SPServices({
			operation: "WebUrlFromPageUrl",
			async: false,
			pageURL: $("#inputURL").val(),
			completefunc: function (xData, Status) {
				$('#siteURL').html($(xData.responseXML).find("WebUrlFromPageUrlResult").text());
			}
		})
		getSiteLists($('#siteURL').text());
	});

	$('#ListSlct').change(function(){
		$("#colSlct").html(waitMessage);
		$().SPServices({
			operation: "GetList",
			webURL: $('#siteURL').text(),
			listName: $(this).val(),
			async: false,
			completefunc: function (xData, Status){
				var outputHTML = "</pre>
"; $(xData.responseXML).find("Fields > Field").each(function(index) { if((index % 6) == 0) { outputHTML += "" };outputHTML +=""; }); outputHTML += "
<table>
<tbody>
<tr>
<td><input type="checkbox" name="" value="+ $(this).attr(" />"+ $(this).attr("DisplayName") +"</td>
</tr>
</tbody>
</table>
<pre>
";
				$("#colSlct").html(outputHTML);
			}
		})
	});
	$('#getSatisfaction').click(function(){
		var qryCAML = "" +n";
		var tableResult = "
" +n"; $('input:checkbox:checked').each(function(){ qryCAML += "tttt"" +n";tableResult += "tttt"" + $(this).attr("ows_"+ this.value+"") + "" +n"; }) qryCAML += "ttt""; tableResult += "ttt"

";
		var resultCode = "";
		resultCode += "var htmlTable = '</pre>
';"; resultCode += "n$().SPServices({"; resultCode += "ntoperation: "GetListItems","; resultCode += "ntasync: false,"; resultCode += "ntlistName: "" + $('#ListSlct').val() + "","; resultCode += "ntwebURL: "" + $('#siteURL').text() + "","; resultCode += "ntCAMLViewFields: "" + qryCAML + "","; resultCode += "ntcompletefunc: function (xData, Status) {"; resultCode += "ntt$(xData.responseXML).SPFilterNode('z:row').each(function() {"; resultCode += "nttthtmlTable += "" + tableResult + "";"; resultCode += "ntt})"; resultCode += "ntthtmlTable += "
<pre>
";";
		resultCode += "nt}";
		resultCode += "n});";
		$('#results').html("<textarea id="resultsCode" rows="20">"+resultCode+"</textarea>");

	});
	//load the default site
	$("#inputURL").val(location.href)
	$("#inputURL").trigger('change');

});

function getSiteLists(tURL){
	$("#ListSlct").html("
Loading...
");
	$().SPServices({
		operation: "SiteDataGetListCollection",
		webURL: tURL,
		completefunc: function(xData, Status){
			//renderList(xData, Status)
			$("#ListSlct").html("
Select a site list...
");
			$(xData.responseXML).find("_sList").each(function() {
				$("#ListSlct").append("
" + $("Title",this).text() + "
");
			});
		}
	});
}

Have a thought or opinion?