This project has moved. For the latest updates, please go here.

Proper use of SPServices

Jul 6, 2012 at 2:52 PM

I brought a problem home to my SP2010 installation, and part of that exploration is another study of where it is safe to presume that, say, global variables are accessible. During my current play, I found what is to me a rather curious phenomenon. In the code below, the alert statement that is not commented out keeps any of this code from running on the page. Comment out that line, and all is fine. Especially if this is something obvious, I'd appreciate a comment.

var xarIDs = [];
$(document).ready(function() {


"); //must be placed after document.ready }); function checkRecord() { $().SPServices({ operation: "GetListItems", listName: "{93BD9044-5347-4FEC-8478-E13BC1F1F2C5}", completefunc: function (xData, Status) { $(xData.responseXML).SPFilterNode('z:row').each(function() { var thisFSObjType = $(this).attr("ows_FSObjType").split(";#")[1]; if(thisFSObjType == 0) { xidRecord = $(this).attr("ows_ID"); //document library record holding original form xarIDs.push(xidRecord); //add any eligible document to the array containing same var formFilename = $(this).attr("ows_LinkFilename"); //ready the actual filename for AJAX use $.ajax( { type:"GET", url:"http://annamaria/sites/test/crms/crmsEmail/" + formFilename, dataType:"html", success: function(data) { doStuff(data, formFilename); }, error: function() {alert("failed to open this eform file name: " + formFilename);} }); } //if this is a file name }); //each() //alert("xarIDs: " + xarIDs); //fine } //completefunc alert("xarIDs (within SPServices): " + xarIDs); //just having this line keeps the whole page code from executing }); //SPServices //alert("xarIDs (end of function): " + xarIDs); //displays after completefunc but no content } //checkRecord
Jul 6, 2012 at 5:48 PM


I think your problem is probably that you aren't doing the call to GetListItems synchronously. This means that the .ajax call may not have the data it needs to work.

It's a little hard to follow your code the way it's formatted, but that would be my first guess.


Jul 8, 2012 at 3:40 PM

Thank you, Marc. You were right about the async:true/async:false situation (only async:false works) but tests show that the Ajax call gets its data just fine under both conditions. The only thing I can determine that does NOT get its data is the global variable (xarIDs) that I'm referencing in the final alert.

My original question was crazy. Although I'm still curious as to why having an alert at that point completely blocks any script activity (including assigning "Hi!" to an existing DIV), putting any code at that particular point (between completefunc and the end of SPServices) is absolutely crazy.

Jul 9, 2012 at 1:25 PM

I don't see all of your script (e.g., DoStuff()), so I can't be sure. Are you defining xarIDs globally? Might it be undefined what you try to alert it?

I think a little debugging in the Developer Tools will show you what's going on.