Lists.asmx web service has  a method called UpdateListItems which can be used to update an item to a custom list. Please see below my code:

Type.registerNamespace('SHP');

SHP.Data.AsyncUpdateItem = function(item,listId,successCallback,failCallback) {

	var xmlHttpRequestBody = '<?xml version="1.0" encoding="utf-8"?>' +
					  		 '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
					  		 '<soap:Body>' +
					  		 '<UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">' +
					 		 '<listName>' + listId + '</listName>' +
				      		 '<updates>' +
					  		 '<Batch OnError="Return">' +
				     		 '<Method ID="0" Cmd="Update">' +
                     		 '<Field Name="ID">' + item["ID"] + '</Field>';
    				 		 for(var property in item)   {
    	              		 	if((item.hasOwnProperty(property) == true) && (property != 'ID')) {
    								xmlHttpRequestBody  += '<Field Name="' + property + '">' +  '<![CDATA[' + item[property] + ']]>' + '</Field>';
    							}
    				  		}
	xmlHttpRequestBody 	  += '</Method>' +
					         '</Batch>' +
				             '</updates>' +
					 		 '</UpdateListItems>' +
					 		 '</soap:Body>' +
                      		 '</soap:Envelope>';

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open('POST',SHP.Resources.lists_ws,true);
	xmlHttp.setRequestHeader('Content-Type','text/xml; charset=utf-8');
	xmlHttp.setRequestHeader('Content-Length',xmlHttpRequestBody.length);
	xmlHttp.setRequestHeader('SOAPAction','http://schemas.microsoft.com/sharepoint/soap/UpdateListItems');
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				var response = xmlHttp.responseXML;
				var errorsInResponse = response.getElementsByTagName("ErrorText");
				if(errorsInResponse.length == 0) {
					successCallback(SHP.Data.SerializeItems(response));
				}
				else {
					var error_message = 'Request status' + xmlHttp.status + 'Error text: ' + errorsInResponse[0].childNodes[0].nodeValue;
					failCallback(error_message);
				}
			}
			else {
					var error_message = 'Request status' + xmlHttp.status + 'Error text: ' + xmlHttp.responseText;
					failCallback(error_message);
			}
		}
	}
	xmlHttp.send(xmlHttpRequestBody);
}

SHP.Data.registerClass('SHP.Data');

You also need to create a class which should act like a configuration class. Update the lists property to url to lists.asmx.

SHP.Resources = {	
	lists_ws:'path to list.asmx'
}

And now, let me provide how to use this function.

var item = new Object();
item["ID"] = idOfItem; // Should be always set for update operation
item["InternalFieldName1"] = "somevalue";
item["InternalFieldName2"] = "somevalue2";

SHP.Data.AsyncUpdateItem(item,"List name",function(results) { }, function(err) { alert(err); });

Advertisements