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() + "
");
			});
		}
	});
}

Similar Posts

4 Comments

  1. Hi Mark, I am using spservices and its great! I have one scenario where I have 15k approved or submitted items that I need to pull in a grid in a paged grid. Now the thershold 5k will error out the getitems. Can I use contentiterator along with spservice to pull the data fast with page numbers? I need great performance so I can not use simple c# based web part.

    1. @Amit:

      Unfortunately, there isn’t any way around the 5k limit in SharePoint Online – yet. Apparently Microsoft is working on it. Also see this post.

      In cases like yours (I’m wrestling with one right now), I use localStorage to keep a copy of the list items, updating them whenever they change.

      M.

  2. HI Marc, is there a preferred method to add this script to a page to display?
    I have tried adding your the script above in a file with jQuery and SPServices-2014.02 and adding a CEWP to the page but nothing displays.
    Do I need to change anything in the script?
    Your links above to the Demo page is dead.
    Thanks.

    1. @Brett:

      I retire that demo page years ago now. It was on a hosted WSS 3.0 instance – a bit out of date! If you have valid references to jQuery and SPServices in the page above the script, you should be good. You might need to do some debugging in the developer tools to see what errors you’re getting. Be sure to check out the other posts I linked to if you are still having problems.

Leave a 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.