1

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

Drkawashima
  • 8,837
  • 5
  • 41
  • 52

2 Answers2

1

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:

  1. $(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.

  2. 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 for success and complete .

    • Complete is defined as: Function( jqXHR jqXHR, String textStatus )
    • Success is defined as: 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) .

Drkawashima
  • 8,837
  • 5
  • 41
  • 52
0

You have to fill in an ID when you call UpdateListItems. See the Cmd="New" example in MSDN.

So in your case, change you Batch element to:

var batch = "<Batch OnError=\"Continue\"><Method ID=\"1\" Cmd=\"New\">" + 
                "<Field Name=\"Title\">" + title + "</Field>" +
                "<Field Name=\"ID\">" + New + "</Field>" +
            "</Method></Batch>";

Where New is a made up ID. You still have to parse the result for the real ID, as you're already doing.

Here's an example of a request to a SP2010 Server list:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <listName>Announcements</listName>
      <updates>
        <Batch OnError="Continue">
          <Method ID="1" Cmd="New">
            <Field Name="ID">998877</Field>
            <Field Name="Title">abcdef</Field>
          </Method>
        </Batch>
      </updates>
    </UpdateListItems>
  </soap:Body>
</soap:Envelope>

And the response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <UpdateListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <UpdateListItemsResult>
        <Results>
          <Result ID="1,New">
            <ErrorCode>0x00000000</ErrorCode>
            <ID />
            <z:row ows_ID="6" details="removed" xmlns:z="#RowsetSchema" />
          </Result>
        </Results>
      </UpdateListItemsResult>
    </UpdateListItemsResponse>
  </soap:Body>
</soap:Envelope>
ErikE
  • 857
  • 8
  • 18
  • Are there any requirements for what the made up ID should be? MSDN doesn't really explain how to use it, only states that it doesn't correspond to a list item id. ( "Each method that is posted contains Field elements that specify the ID of the item and the new field value for the item. The field ID does not correspond to the index of the item in the collection of items for the list.") – Drkawashima Jan 18 '13 at 15:22
  • Right, that's why I used a Guid. I'm not sure if there's an easier (api) way, but [this question's answers](http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript) show some ways to generate a guid in JavasScript. – ErikE Jan 18 '13 at 17:03
  • Looks like you can give it a fixed value too (like 'New'). Unfortunately the documentation is poor and I wouldn't be surprised if the behavior can vary a bit between the versions and editions of SharePoint. Guid works for me from WSS 3.0 to, SP2013 Enterprise, but it may wasting a few bytes and CPU cycles. – ErikE Jan 19 '13 at 18:01
  • Gave it a try using a random int as ID but still no data in the z:row eelement. If I check the childnodes of data.ResponseXML I can find 3 nodes: error, id and z:row. Error is 0x0000 (which means no error occurred), id is empty and z:row is also empty. I tried it on 3 different lists and got the same result. – Drkawashima Jan 21 '13 at 10:32
  • Strange that z:row is empty. I don't think that setting ID is the problem, setting it to a random id, that doesn't exists, works for me too. I added a request/response to my answer. Could you take a look (using Fiddler or your browser network trace) and look if there's anything different? – ErikE Jan 21 '13 at 12:46
  • Whoa! Looking at the response in Fiddler I can tell that z:row actually contains all the expected data! (such as ows_ID etc) The problem just seems to be that I can't find that data in the js object I'm recieving in my onSuccess callback function. I've browsed it manually, and searched it using find('z:row'), find(' – Drkawashima Jan 21 '13 at 13:29
  • ...find("row"), find('z\\:row'), find('z\\:row, row'). Tried all the recommended variants of the find function and they never find anything. z:row also appears empty while looking at the object in the chrome debugger. So it seem that the js object doesn't contain all the data from the response.. :/ – Drkawashima Jan 21 '13 at 13:39
  • [Jan Tielen's blog entry] (http://weblogs.asp.net/jan/archive/2009/04/09/calling-the-sharepoint-web-services-with-jquery.aspx) suggests you set the dataType to xml, not text/xml. I haven't used the webservices from JavaScript, have you looked at [SPServices] (http://spservices.codeplex.com/) for a jQuery library wrapping the SharePoint services? If you're using SharePoint 2010 or newer you could look at the [JavaScript Client Side object model] (http://msdn.microsoft.com/en-us/library/hh185015(v=office.14).aspx). – ErikE Jan 21 '13 at 13:54
  • I posted a new answer which explains why I had problems with z:row.After fixing that bit I found that the lack of an ID in my command didn't seem to matter. You can create new items just fine without specifying an ID. So it doesn't seem to be required, and I'm curious if it's even needed. I've asked about it on MSDN to try to find something out. The documentation sure is poor – Drkawashima Jan 24 '13 at 13:31
  • Interesting, didn't know that. I forgot about this, but there is sometimes a bit more documentation in the 'Protocol Documents' look http://msdn.microsoft.com/en-us/library/cc313068(v=office.12).aspx. It says in section 3.1.4.31.2.1 that MUST be ignored in a 'New', so there you have it. – ErikE Jan 24 '13 at 15:46