SPServices.SPAddMultipleListItems New Feature

May 27, 2011 at 11:24 AM

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
Coordinator
May 27, 2011 at 3:11 PM

Dan:

This would allow you to add identical items to the list, but I would think it would be unusual to want to do that. I suppose with SPPostIt you are adding one item per recipient?

M.

May 27, 2011 at 4:32 PM

No, this is during the creation of a category list and I am adding some default categories. I actually have many uses for it! I could adapt it to ensure that duplicate items were not added, but since I was adding the items I knew I would not be adding duplicates. I know I would be using it alot when creating lists or adding dummy/example data to a list. Just thought others might want it just in case!

Coordinator
May 27, 2011 at 4:50 PM

Oh, I see. You're using valuepairs to contain the data for a set of list items. You're creating one method per valuepair, so adding different items with different values. Duh. I was looking at the code wrong.

This is nice. Sort of like a Web Services way to do a bulk load, for which most people would use Powershell.

M.

May 27, 2011 at 5:06 PM

Yes, because I can't use Powershell! :)

But, in all honesty I really like this approach for what I use it for. Feel free to use as you wish!

Dan

May 28, 2011 at 11:31 AM

I'm liking SPMultiUpdate, but I'm thinking there needs to be a batchSize option.  Sometimes I need to process more than 200 records and it's very taxing to do that via Web Services.  If I could stop the for loop when the Method reaches 200, I'd like for that batch to be sent to update, and then reset the counter to process more items.

Coordinator
May 28, 2011 at 12:45 PM

Well, the right answer here is that you probably shouldn't be using the Web Services to do massive updates. However, I know that you are working in some environments where you don't have a choice.

Can you explain why this 200 thing seems like a benefit to you?

M.

Coordinator
May 31, 2011 at 5:41 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.