Trying to create a new list item in and store the created item’s ID in a variable for later usage. (in SharepointOnline 2010)
I’m able to create the item perfectly fine but not able to get the ID of the resulting object.
My current solution is to make an ajax request using UpdateListItems to create the item and then in the success callback function try find and ID in the resulting object.
var onsuccessArg = function (data) {
$(data).find('z\\:row').each(function () {
savedItemID = $(this).attr('ows_ID');
});
};
So the list item is created successfully and onSuccess is called. But the each clause is never entered.
At first I thought there was a browser issue with find('z\\:row') but I’ve looked at the data object it in the chrome debugger and I can see that while a <z:row> element does exist it does not contain any child nodes! So it’s like the service hasn’t returned any items, even though it did create a list item successfully.
Why could that be?
I Should I be able to get the created list item id this way, right? Am I just missing something or is my approach incorrect?
Could there be some weird issue with the server that’s causing this? I think that this code has been working fine previously
Edit: For some reason not able to paste the entire code into the post. So here’s a pastebin instead http://pastebin.com/iGrvytgn
Like Eric said, the ID field SHOULD be specified but it doesn’t seem to be required. I’ve managed to execute the UpdateListItems command perfectly fine both with or without it…
But I found 2 other problems with the code in my first post:
$(data).find('z\\:row')doesn’t work in chrome. Had to append the line$(data.responseText).find('z\\:row').Turns out that chrome places data in a different property than other browsers. It uses responseText property rather than responseXML property. and `$(data).find(‘z\:row’) will not find anything in the responseText property.
This was quite the head scratcher, cause when I checked the chrome debugger I could still see that responseXML property was present and contained all the expected nodes, just missing any data such as ows_ID.
The callback function (
function(data,status)) didn’t have to correct parameters to be used as the success function of the ajax call. The response data of an ajax call is placed in the jqXHR object. I thought this object was always the first parameter, but turns out the parameter list is different forsuccessandcomplete.Function( jqXHR jqXHR, String textStatus )Function( Object data, String textStatus, jqXHR jqXHR )So to use my callback as the success function it’s header should be
function(obj,status,data).