Is it possible to use a variable to store the value pairs for a SPServices.UpdateListItems call?

Aug 25, 2013 at 11:00 PM
I have got SPServices.UpdateListItems to work when explicitly setting the value pairs parameter in the function call, but when I try to set the value pairs as a variable and reference that variable in the same function call I get an error as follows:
 <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.</faultstring><detail><errorstring xmlns="http://schemas.microsoft.com/sharepoint/soap/">Cannot access a closed Stream.</errorstring></detail></soap:Fault></soap:Body></soap:Envelope>
I am setting my variable as follows:
$valuePairStr = '[["JobTitleLookUp",$jobTitleLookUp.val()],["Description_x0020_of_x0020_Dutie", $descDuties.val()],["Pay_x0020_Basis", $payBasis.val()],["Contracted_x0020_Hours",$contHours.val()],["Advertisement_x0020_Date",$adDateformatted],["Application_x0020_Close_x0020_Da",$appCloseDateformatted]],'
...and my SPServices call is as follows:
$().SPServices({
        operation: "UpdateListItems",
        async: false,
        batchCmd: "Update",
        listName: "Manage Recruitment Process Tasks",
        ID: $myID,
        valuepairs: $valuePairStr,
        completefunc: function(xData, Status) {
            alert(xData.responseText + Status);
            if (Status === "success"){
                alert("Your updates have been saved");
                dispCustomButton($btnPlaceJobAd);
            }else{
                alert("Your updates could not be saved.  Please check and retry");
                showCustomButton(false);
            }
            
        }
    })
Have I goofed somewhere, or am I not able to use a variable string in lieu of the value pairs paremeter?

Thanks

Simon
Aug 26, 2013 at 6:00 PM
Edited Aug 26, 2013 at 6:01 PM
Simon,

I regularly use variables for various SPServices functions, but I insert them inside the parameter strings. I don't believe that you can create a single variable to represent the entire parameter string; only the individual variables. In your case, for example, perhaps use code similar to the following:
$().SPServices ({
    operation: "UpdateListItems",
    async: false,
    batchCmd: "Update",
    listName: "Manage Recruitment Process Tasks",
    ID: $myID,
    valuepairs: [["JobTitleLookup", $jobTitleLookup.val()], ["Pay_x0020_Basis", $payBasis.val()], .... etc etc ],
    completefunc: function(xData, Status) {
            alert(xData.responseText + Status);
            if (Status === "success"){
                alert("Your updates have been saved");
                dispCustomButton($btnPlaceJobAd);
            }
            else
            {
                alert("Your updates could not be saved.  Please check and retry");
                showCustomButton(false);
            }
            
        }
    })
   
Aug 26, 2013 at 9:27 PM
Hi coeddyiv,

Thanks; that's kind of where my thinking's at at the moment.

To add a bit of colour to my scenario, I am using one task list to handle a number of stages of a business process, with different pieces of information being gathered at different stages. I'm using the UpdateListItems function to improve the UX - opposed to the OOTB save and then re-edit the task - so am trying to use the one 'update' function for all stages of the process, hence my desire to only pass those fields needing to be updated into the function at the appropriate time and so parameterise the value pairs.

If its a case that this function cannot work in this way then I'll resort to one set of value pairs and pass in dummy variables for the fields yet to be set.
Coordinator
Aug 29, 2013 at 10:53 PM
Hi fairfieldbusserv,

The valuepairs expects an actual JavaScript array, not a string that represents one. If you remove the single quotes around the variable's value (and the stray comma at the end) it should work.
$valuePairStr = [["JobTitleLookUp",$jobTitleLookUp.val()],["Description_x0020_of_x0020_Dutie", $descDuties.val()],["Pay_x0020_Basis", $payBasis.val()],["Contracted_x0020_Hours",$contHours.val()],["Advertisement_x0020_Date",$adDateformatted],["Application_x0020_Close_x0020_Da",$appCloseDateformatted]];
Aug 30, 2013 at 8:20 PM
In my opinion one of your variables contains illegal XML characters.
The simplest solution would be to wrap such variables into CDATA. look here: http://www.w3schools.com/xml/xml_cdata.asp
I personally have a cdata function in my JavaScript library, and use it as follows:
function cdata(str) {
   if (str && str != ""){
        return = "<![CDATA[" + str + "]]>";
    }
    else
       return "";
}   
["Description_x0020_of_x0020_Dutie", cdata($descDuties.val())]

Regards
Michal
Aug 31, 2013 at 2:29 PM
Michal,
Did not know you could use CDATA when defining a value for update to a column. Good tip. Thanks for sharing.

_____
Paul

Sent from mobile device.

Sep 3, 2013 at 9:59 PM
Edited Sep 3, 2013 at 9:59 PM
joshmccarty wrote:
Hi fairfieldbusserv,

The valuepairs expects an actual JavaScript array, not a string that represents one. If you remove the single quotes around the variable's value (and the stray comma at the end) it should work.
$valuePairStr = [["JobTitleLookUp",$jobTitleLookUp.val()],["Description_x0020_of_x0020_Dutie", $descDuties.val()],["Pay_x0020_Basis", $payBasis.val()],["Contracted_x0020_Hours",$contHours.val()],["Advertisement_x0020_Date",$adDateformatted],["Application_x0020_Close_x0020_Da",$appCloseDateformatted]];
Thanks @joshmccarty,

It's those simple things that are the most frustrating; all now working fine.
Sep 4, 2013 at 7:27 AM
Sorry, I didn't see the quotes around the definition of the ValuePairs array.

Of course the variable needs to be an array of arrays not a string.
To understand better:

var valuePairs = [];;

//add Title
valuePairs.push(["Title", "This is the title"]);

//add FieldA
valuePairs.push(["FieldA", "This is the field A"]);

Regards
Michal
Coordinator
Sep 4, 2013 at 12:33 PM
I'll admit that it's a little clunkier than it has to be, but it works. If I were to build it now, it'd be an object rather than an array of arrays.

M.