I am trying to use REST API approach in javascript to update a sharepoint list item where I have an id. So I use GET and POST to create records, but per MSDN article it looks like the way to do this is combination of MERGE and POST but I am having trouble connecting the dots. So it appears under MERGE and PUT Operations section there is an entity that gets created in a basic ajax/POST method, and then the update utilizes the same approach to update, but I am trying to find an approach that will allow me to update without the post method....so this may be straight forward but....
appreciate any help with this as always
UPDATE: so I probably need to ellaborate on this a bit:
I use REST to pull all the time because for whatever reason in my environment, it seems to be consistently the quickest returning data between it, COM and SPServices. Been using this type of approach:
getData(thisUrl, thisCallback);
function getData(url, callback) {
try {
var request = new Sys.Net.WebRequest();
request.set_httpVerb("GET");
request.set_url(gridUrl);
request.get_headers()["Accept"] = "application/json";
request.add_completed(callback);
request.invoke();
} catch (e) {
alert(e.message);
}
}
function thisCallback(response, eventArgs) {
var resultData = JSON.parse(response.get_responseData());
for (var i = 0; i < resultData.d.results.length;i++) {
// get my data and process
}
}
}
this works awesome so I just wanted to explore the other basic CRUD operations. found this MSDN page: http://msdn.microsoft.com/en-us/library/ff798339.aspx
Example below works well for me for creating a new list item (taken from link):
var url = 'http://localhost/sites/sharepointlist/_vti_bin/listdata.svc/InventoryLocations';
var inventoryLocation = {};
// Insert a new Part location.
inventoryLocation.PartId = $('#hidPartId').val();
inventoryLocation.BinNumber = $('#binText').val();
inventoryLocation.Quantity = $('#quantityText').val();
var body = Sys.Serialization.JavaScriptSerializer.serialize(inventoryLocation);
$.ajax({
type: 'POST',
url: url,
contentType: 'application/json',
processData: false,
data: body,
success: function ()
{
alert('Inventory Location Saved.');
}
});
so the next part I wanted to learn is the updating, which per this article seems to be a combination of MERGE and POST?:
var locationId = $('#hidLocationId').val();
var url = 'http://localhost/sites/sharepointlist/_vti_bin/listdata.svc/InventoryLocations';
var beforeSendFunction;
var inventoryLocationModifications = {};
// Update the existing Part location.
url = url + "(" + locationId + ")";
beforeSendFunction = function (xhr)
{
xhr.setRequestHeader("If-Match", inventoryLocation.__metadata.ETag);
// Using MERGE so that the entire entity doesn't need to be sent over the wire.
xhr.setRequestHeader("X-HTTP-Method", 'MERGE');
}
inventoryLocationModifications.BinNumber = $('#binText').val();
inventoryLocationModifications.Quantity = $('#quantityText').val();
var body = Sys.Serialization.JavaScriptSerializer.serialize(inventoryLocationModifications);
$.ajax({
type: 'POST',
url: url,
contentType: 'application/json',
processData: false,
beforeSend: beforeSendFunction,
data: body,
success: function ()
{
alert('Inventory Location Saved.');
}
});
so this i can get to work if I first create the item, then turn around and update the item using this exact approach, but I am wondering how you would take this approach to just update an item that already exists? In order to use the above do I have to create the item first to get the inventoryLocation so the ._metadata.ETag doesn't error? Is there another way?
I understand I am probably just lacking some context on this, I just haven't been able to find anything yet that helps me get it.....always appreciated!