UpdateListItems - Adding a new item

Sep 27, 2010 at 8:58 PM

I didn't find the documentation clear enough on just adding a simple list item. Here is an example of adding a new list item from a simple feedback type form.

 

function CreateNewItem(subject, message) {
    $().SPServices({
        operation: "UpdateListItems",
        async: false,
	batchCmd: "New",
        listName: "Feedback",
	    valuepairs: [["Title", subject], ["Message", message]],
        completefunc: function(xData, Status) {
			alert("completed");
        }
    });
}


$(document).ready(function() {
    $("#feedback-submit input").click(function() {
	      
		var subject = $("#feedback-subject-input input").val();
		var message = $("#feedback-message-input textarea").text();

		CreateNewItem(subject, message);		

    });
}); 

Coordinator
Sep 27, 2010 at 9:41 PM

Tom:

Sorry that the docs weren't helpful enough for you. I like this example, and I'll add it do the docs for others!

Note that the valuepairs notation is something I built into SPServices; the Web Service requires the Batch notation as shown in the docs:
http://spservices.codeplex.com/wikipage?title=UpdateListItems&referringTitle=Lists 

M.

Sep 27, 2010 at 10:26 PM

Marc, no worries, i was able to figure it out. Hope this will help other trying to do something similiar. Love the script! keep up the good work.

td

Nov 8, 2010 at 5:31 AM

Hi Tom,

Thanks for the simple example.

I am trying to add an item to a list. What I observed is if there is a special character in the string the UpdateListItems is failing to create an item.

().SPServices({
operation: "UpdateListItems",
async: false,
batchCmd: "New",
listName: "ListName",
webURL: "/sites/myTestsite",
valuepairs: [["Title", Title],["FirstColumn", FirstColumn],["SecondColumn", SecondColumn]],
completefunc: function(xData, Status) {
}

In the above example if FirstColumn string contains a string like 'Rajdeep & Chakraborty' its failing to create an item in ListName list.

Can you please help me in this regard ?

Thanks

Nov 8, 2010 at 2:38 PM

You definately can't use special characters like & and " in the value listname or valuepairs. Depending on what I'm doing i usually html encode or escape() them. For your list try 'Rajdeep & Chakraborty'

Coordinator
Nov 8, 2010 at 3:15 PM
I see that you posted your question on the UpdateListItems documentation page as well; I'm going to delete it from there.
The need to escape certain characters in your data is just a standard Web thing, and doesn't have anything to do with SPServices per se.
M.