UpdateListItems doesn't support adding new items

Oct 29, 2009 at 9:42 PM
Edited Oct 29, 2009 at 9:43 PM

After looking at the code for the jQuery Library for Sharepoint Web Services, I noticed that the UpdateListItems doesn't support adding new list items.

I modified my local copy with the following code to make it work.  It isn't battle tested, but it is working for me.

Around line 484 (in $.fn.SPServices.defaults):

batchCmd: "Update",
 

And then around line 229, I updated the case for UpdateListItems:

case "UpdateListItems":
	SOAPEnvelope.payload += wrapNode("listName", opt.listName);
	if(opt.updates.length > 0) {
		SOAPEnvelope.payload += wrapNode("updates", opt.updates);
	} else {
		SOAPEnvelope.payload += "<updates><Batch OnError='Continue'><Method ID='1' Cmd='" + opt.batchCmd + "'>";
		for (i=0; i < opt.valuepairs.length; i++) {
			SOAPEnvelope.payload += "<Field Name='" + opt.valuepairs[i][0] + "'>" + opt.valuepairs[i][1] +  "</Field>";
		}
		SOAPEnvelope.payload += "<Field Name='ID'>" + opt.ID + "</Field>";
		SOAPEnvelope.payload += "</Method></Batch></updates>";
	}
	break;

Let me know what you think.

 

 

Coordinator
Oct 30, 2009 at 3:09 AM
Edited Oct 30, 2009 at 11:18 AM

You stumbled on something that I had put into the library way back in the beginning when I was first trying to prove the ideas out to myself.  My intention with the valuepairs/ID was to give a quicker way to update fields on a single item quickly without having to build the full updates XML.  I left it in, thinking that I would expand on the CAML-less idea later, but I haven't really documented it.  The updates parameter is the only one that the UpdateListItems operation takes natively.

What you're doing here certainly will work if you find it easier to follow than the full updates syntax.

M.

Oct 30, 2009 at 10:59 AM

IMHO, the "updates" marker wasn't isn't very intuitive when you have other parameters starting with "CAML."  I had no idea what it did until I looked at the code for the library.   Maybe if it was called CAMLquery or CAMLupdates then it would be more apparent (to my simple mind anyhow.)  Also your library does a great job setting up the CAML with the name value pairs, so I wanted to avoid writing out the full CAML if I could just get away with switching out the batch CMD.

Anyhow, you own the library, so what you do with it is up to you.  But personally, I would roll a change out that allows a person to specify New versus Update, and change "updates" to something more intuitive.

Coordinator
Oct 30, 2009 at 11:18 AM

Actually, the updates brilliance isn't mine, but Microsoft's. ;=)  If you read my first article at EndUserSharePoint.com, A jQuery Library for SharePoint Web Services (WSS 3.0 and MOSS): Why and Why Now?, you'll see that I decided to use exactly the parameter names that Microsoft uses so that folks who are used to work with the Web Services wouldn't be doing things differently.  Then I want to add overrides to the commonly used operations like the one you spotted for regular folks (like me).

When in doubt on the parameters, always check the MSDN documentation, in this case: Lists.UpdateListItems Method

But you're right on the utility of your suggestion, so I'll add it into the next release! 

Thanks,
M.

Coordinator
Oct 30, 2009 at 11:19 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Oct 30, 2009 at 1:52 PM

Ahh, OK.  Many thanks for the explanation and for providing a great and helpful library.

Coordinator
Oct 30, 2009 at 2:00 PM

No problem at all.  These discussions are useful for me to use in tightening up the documentation, thinking of new features, etc.

M.