While developing SPPostIt, I discovered I needed to create a list and then add multiple items to the list. It appeared to me that I could also use this elsewhere so I copied the UpdateMultipleListItems and created this one. Feel free to use/modify. Just
posting if anyone needs it.
// SPAddMultipleListItems allows you to add multiple items in a list.
$.fn.SPServices.SPAddMultipleListItems = function(options) {
var opt = $.extend({}, {
webURL: "", // [Optional] URL of the target Web. If not specified, the current Web is used.
listName: "", // The list to operate on.
batchCmd: "New", // The operation to perform. By default, New.
valuepairs: [], // 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; i++) {
batch += "<Method ID='" + i + "' Cmd='" + opt.batchCmd + "'>";
batch += "<Field Name='" + opt.valuepairs[i][0] + "'>" + opt.valuepairs[i][1] + "</Field>";
batch += "</Method>";
}
batch += "</Batch>";
// Call UpdateListItems
$().SPServices({
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();
}
}
});
}; // End $.fn.SPServices.SPAddMultipleListItems
Comments: I'm trying to use your 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? ``` <script type="text/javascript"> $("input[id$='idSubmit']").click(function(){ postFunction(); }); 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> ```
posting if anyone needs it.
// SPAddMultipleListItems allows you to add multiple items in a list.
$.fn.SPServices.SPAddMultipleListItems = function(options) {
var opt = $.extend({}, {
webURL: "", // [Optional] URL of the target Web. If not specified, the current Web is used.
listName: "", // The list to operate on.
batchCmd: "New", // The operation to perform. By default, New.
valuepairs: [], // 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; i++) {
batch += "<Method ID='" + i + "' Cmd='" + opt.batchCmd + "'>";
batch += "<Field Name='" + opt.valuepairs[i][0] + "'>" + opt.valuepairs[i][1] + "</Field>";
batch += "</Method>";
}
batch += "</Batch>";
// Call UpdateListItems
$().SPServices({
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();
}
}
});
}; // End $.fn.SPServices.SPAddMultipleListItems
Comments: I'm trying to use your 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? ``` <script type="text/javascript"> $("input[id$='idSubmit']").click(function(){ postFunction(); }); 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> ```