Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to create a new list item and store the created item's ID in a variable for later usage.

So I'm using UpdateListItems and making an ajax call and then in the success callbakc function ( onSuccess(data) ) I want to find the ID in the resulting object (data). The solution successfully creates a list item, the only problem is that I can't get the resulting ID. . When I try to traverse through the resulting data object I can't find any ID. If I look at the data object in the chrome debugger and traverse down the childNodes I can see that it contains all necessary elements. For example - the z:row element Exists, however it contains 0 child nodes so I can't get any ID from it.

    var savedItemID;

    function CreateNewItem(title, listName) {
        var batch =
            "<Batch OnError=\"Continue\"><Method ID=\"1\" Cmd=\"New\"><Field Name=\"Title\">" + title + "</Field></Method></Batch>";

        var soapEnv =
            "<?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>" + listName + "</listName> \
                 <updates> \
                   " + batch + "</updates> \
               </UpdateListItems> \
             </soap:Body> \
           </soap:Envelope>";

        var beforesendArg = function (xhr) {
            xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");

        };

        var onsuccessArg = function (data) {
            $(data).find('z\\:row').each(function () {
                savedItemID = $(this).attr('ows_ID');
            });
        };

        var onfailArg = null;

        AjaxCall("POST", siteCollection + "/_vti_bin/lists.asmx", false, soapEnv, "text/xml; charset=utf-8", "xml", beforesendArg, onsuccessArg, onfailArg);


    }


    function AjaxCall(typeArg, urlArg, cacheArg, dataArg, contentTypeArg, datatypeArg, beforesendArg, onsuccessArg, onfailArg) {

        //set a default contenttype if not specified
        if (contentTypeArg === null) {
            contentTypeArg = 'application/x-www-form-urlencoded';
        }

        if (onfailArg === null) {
            onfailArg = function (XMLHttpRequest, textStatus, errorThrown) {
                alert('AjaxCall has failed with the following error message: ' + errorThrown);
            }
        };

        $.ajax({
            type: typeArg,
            cache: cacheArg,
            url: urlArg,
            data: (dataArg),
            contentType: contentTypeArg,
            dataType: datatypeArg,
            beforeSend: beforesendArg,
            success: onsuccessArg,
            error: onfailArg
        });

    }

Is my approach correct? Should I be able to get the created list item id this way? Am I just missing something?

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.