Trying to use SPAddMultipleListItems

May 15, 2014 at 3:37 PM
I'm trying to use the example code but it does not appear to be working. I added the function to a click event. Not even the alert at the top works. Any ideas?
function postFunction() { //main function
    alert("it worked");
    $.fn.SPServices.SPAddMultipleListItems = function(options) { //add multiple list items function

    var opt = $.extend({}, {
        webURL: "",         // [Optional] URL of the target Web.  If not specified, the current Web is used.
        listName: "{8EC40D21-B767-462C-BD20-5E27AFD583C8}",       // The list to operate on.
        batchCmd: "New",    // The operation to perform. By default, New.
        valuepairs: [["CustomerID","1"]],     // Valuepairs for the update in the form [[fieldname1, fieldvalue1], [fieldname2, fieldvalue2]...]
        completefunc: null, // Function to call on completion of rendering the change.
        debug: false        // If true, show error messages;if false, run silent
    }, options);

    var i;
    var fieldNum;
    var batch = "<Batch OnError='Continue'>";
    for (i = 0; i < opt.valuepairs.length; ++;) {
        batch += "<Method ID='" + i + "' Cmd='" + opt.batchCmd + "'>";
        batch += "<Field Name='" + opt.valuepairs[i][0] + "'>" + opt.valuepairs[i][1] + "</Field>";
        batch += "</Method>";
    }
    batch += "</Batch>";
       
$().SPServices({ //update list items
        operation: "UpdateListItems",
        async: false,
        webURL: opt.webURL,
        listName: opt.listName,
        updates: batch,
        completefunc: function(xData, Status) {
            // If present, call completefunc when all else is done
            if (opt.completefunc !== null) {
                opt.completefunc();
            }
        }
    }); //update list items

}; //add multiple list items function
} //main function
</script>
Coordinator
May 15, 2014 at 3:45 PM
Because you have everything wrapped in a function, that function won't fire unless you call it.

There are a number of things you're doing in your script that don't make a lot of sense. You're defining the SPAddMultipleListItems function, but also never calling that.

I'm not sure what "example code" you're using, but you might want to revisit it.

M.
May 15, 2014 at 4:05 PM
I am assigning the click function to a button. If I remove the SPAddMultipleListItems code, the alert fires. When I add it back, it does not.

Here is the code I used to assign the function above to the click event of the button.
$("input[id$='idSubmit']").click(function(){
    postFunction();
});
The sample code I used was from your new feature post (https://spservices.codeplex.com/workitem/8719). I just entered in my site specific info. What is the recommended use of this function? Do I even need to wrap it into another function or should I just call the "options" function? Thank you very much for your help!
Coordinator
May 15, 2014 at 4:19 PM
That item in the Issue Tracker is a suggestion for an enhancement to SPServices, which would add the new function SPAddMultipleListItems. It's not in SPServices today.

As such, the way it is structured is that it would be added into the SPServices library as a function. If you want to use it in the context you seem to be trying, you'll need to restructure the code.

M.
May 15, 2014 at 4:23 PM
Well I feel sheepish! Thanks for the heads up!