4

I am using SharePoint 2013 Enterprise version. I am using below rest api code to add the record I kept below code in common js file

function CreateListItemWithDetails(listName, webUrl, newItem, success, failure) {
    var item = newItem;

    $.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()
        },
        success: function (data) {
            success(data);
        },
        error: function (data) {
            failure(data);
        }
    });
}

// Get List Item Type metadata
function GetItemTypeForListName(name) {
    return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}

Now I am using below code on my button click

var listName = "GatePass";
                        var itemType = GetItemTypeForListName(listName);
                        var area = new Array();
                        $.each($("input[name='Building']:checked"), function () {
                            area.push($(this).val());
                        });
                        var newItem = {
                            "__metadata": { "type": itemType },
                            "Title": $("#txCompanyName").val(),
                            "Area": area.join(", "),
                            "FromDate": $("#fromDate").val()
                        };
                        CreateListItemWithDetails(listName, _spPageContextInfo.webAbsoluteUrl, newItem, function () {
                            alert("New Item has been created successfully.");
                        }, function () {
                            alert("Ooops, an error occured. Please try again.");
                        });

Data's are adding successfully. I can see newly added data in my firebug console also. I know also that success(data) is the responsible for data. But I am not getting how to use that data in another function. Kindly somebody can help.

4

You can get ID of newly created item in success function which you can use later. Please refer below success function :

function CreateListItemWithDetails(listName, webUrl, newItem) {
    var item = newItem;
    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()
        }
    });
}

CreateListItemWithDetails(listName, webUrl, newItem)
    .then(function(data){
        //success callback
        var NewlyCreatedItemId = data.d.ID;
    }, function(data){
        //failure callback
    });
  • Yes, I am getting ID in success function. Thanks rohit – Ankur Hambir Mar 7 '16 at 12:43
  • So where should put this code in my js file or in calling function. If you can give full snippet – Milind Mar 7 '16 at 12:46
  • @Milind i have modified your 'CreateListItemWithDetails' and 'CreateListItemWithDetails' functions in my answer. You can get NewItemId in success callback as i have shown. Please try to implement this way, And let me know if its working for you. – Rohit Waghela Mar 7 '16 at 15:32
  • I will try it tomorrow, if it works then I will mark it answer :) Thank you for your help – Milind Mar 7 '16 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.