Dynamically assign value pairs...

Mar 3, 2011 at 1:56 AM

I had a hoot of a time today trying to subvert a potential deal breaker with a script.  I'm making a web service call that creates a new item.  Within this new item are several people picker fields that aren't required.  I noticed when I queried this list for these values and then took them into the next part of the script (clone the item into the same list), if these fields weren't filled out ( undefined ), then my web service call would fail.  So trying to be smarter than the average bear, I tried to assign the value pairs dynamically:

var valuePairs = "[[\"Title\", " + personnelNum + "], [\"Agency\", " + agency + "], [\"ClassCode\", " + classCode + "], [\"PositionNumber\", " + positionNumber + "], [\"PositionTitle\", " + positionTitle + "], ";

if ( approver1 !== undefined ) {
	valuePairs += "[\"Approver1\", " + approver1 + "], ";
}
if ( approver2 !== undefined ) {
	valuePairs += "[\"Approver2\", " + approver2 + "], ";
}
if ( approver3 !== undefined ) {
	valuePairs += "[\"Approver3\", " + approver3 + "], ";
}
//Get rid of front and end spaces
valuePairs = jQuery.trim(valuePairs);
//Trim trailing comma
valuePairs = valuePairs.slice(0, -1);
//Append bracket
valuePairs += "]";

$().SPServices({
	operation: "UpdateListItems",
	async: false,
	batchCmd: "New",
	listName: "Eval Forms",
	valuepairs: valuePairs,
	completefunc: function(xData, Status) {
		//alert(xData.responseXML.xml);
		$(xData.responseXML).find("[nodeName='z:row']").each(function() {
			newItemID = $(this).attr("ows_ID");
			GetEvalQuesitonTemplates(newItemID);
			return newItemID;
		});
	}
});

That consistently failed, so I opted to send in the new item request by building up some CAML.  I feel it fails b/c of the way the "[" is being handled.  When I concat everything together, it's one giant string.  It looks right, when finished, but it's not what the operation calls for.  I think the "["'s are represented differently when you type them out within the SPService call and aren't handled as a string.  So, no brownie points for me trying to be smarter than the average bear.  I did try a whole host of permutations of the above script as well.  None of which worked and I'm blaming the bracket.

What do you think Marc?

Coordinator
Mar 3, 2011 at 2:05 AM
Matt:

valuepairs is an array of arrays (each with two elements). You're trying to pass in a string, so SPServices will parse it incorrectly.

Build up your values as an array and this should work.

M.
>