sharepoint Retrieve an ID of newly created item in SharePoint list


Example

This example shows how to retrieve an ID of a newly created item using SharePoint REST API.

Note :

listName - This variable contains name of you list.

newItemBody - This will be your request body for adding new item in list.

e.g. var newItemBody = { __metadata: { 'type': 'SP.Data.MyListNameItem' }, Title: 'Some title value' };

function CreateListItemWithDetails(listName, newItemBody) {

    var item = newItemBody;
    return $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        }

    });
}

CreateListItemWithDetails(listName, newItemBody)
    .then(function(data){
        //success callback
        var NewlyCreatedItemId = data.d.ID;
    }, function(data){
        //failure callback
    });