Store GetListItems in a global variable?

Sep 3, 2012 at 3:33 AM
Edited Sep 3, 2012 at 3:33 AM

What is the most straightforward way to directly access or return the results of an array variable inside the .each(function) of a GetListItems completefunc without sending it to a CSS element in the document first?

The thought is to have only one SPServices call initially for a very short list which other client side code could loop through the array as needed.

Example (spservices 0.7.1a): 

var arr = [];
$().SPServices({
	operation: 'GetListItems',
	listName: 'VeryShortList',
	CAMLQuery: '',
	CAMLViewFields: '',
	CAMLRowLimit: 0,
	completefunc: function getXML(xData, Status) {
          	var k = 0;
		$(xData.responseXML).SPFilterNode('z:row').each(function() {
			var id	= $(this).attr('ows_ID'),
			title	= $(this).attr('ows_Title');
			
			arr[k] = {id:id, title:title};
			k = k + 1;
                        // push the array here ....?
		});
		// return resulting "arr" here ...?
	}
});
Sep 3, 2012 at 5:24 AM

I found if I define the array before the GetListItems operation like you did, and push the values inside the .each() function, it works.  However I have not done the return statement inside SPServices.  I have put it after the SPServices call. 

Also, I believe you might be able to replace the statement incrementing k, by putting k inside the function parameters.  As in .each(function(k) {

Sep 3, 2012 at 8:48 AM

For me, an alert(arr[0]); after pushing array values from inside the .each() function using something like arr.push(title); results in an 'undefined', while an alert(arr[0]); after the .each() function works fine. Any thoughts on what I might be missing?

Delighted with the suggested "k". 

Sep 3, 2012 at 7:34 PM
Edited Sep 3, 2012 at 7:37 PM

The array initialization looks different than what I use.  I am not a javascript expert, and that may work, but this is what I had working in some of my code:

function lastupdate(R) {

var a = new Array(); // stores mod date values from associated tables
var d = new Date(); // holder for ea date

var myQuery = "<Query><Where><Eq><FieldRef Name='rfc_id' /><Value Type='Number'>"+R+"</Value></Eq></Where></Query>";

// get the schedule items
 $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "tbl_A",
    CAMLViewFields: "<ViewFields><FieldRef Name='Modified' /></ViewFields>",
	CAMLQuery: myQuery,
	completefunc: function (xData, Status) {
	$(xData.responseXML).find("[nodeName='z:row']").each(function() {
          var d = $(this).attr("ows_Modified");
          a.push(d);
          
      });
    }
  });  // out of SPServices

a.sort();
 
return a[a.length-1]; // last item in array of dates should be the latest.
}

Note, using pre-0.7.1 version of SPServices (.find not .SPfilternode, but will work with 0.7.1 and .SPfilternode).

This is a subset of the code... I use six GetListItems calls to collect the @Modified dates of all child records of a parent record, so I can display the overall last modified date.

Sep 3, 2012 at 9:23 PM

It works with async: false. Apparently async: false is not the default I imagined it to be. Thanks for the example.

The array initialization var a = []; just uses an array literal instead of an array constructor. See http://answers.oreilly.com/topic/2151-when-to-use-the-array-literal-notation-in-javascript/

Coordinator
Sep 5, 2012 at 2:10 PM

What about using the SPXmlToJson function?

M.