Insert List Item in a parent list by using REST API


  • I have written the following code in a sharepoint hosted app to insert a record in a list which exists in the host site.

    var projectItems = [
        { Title: "SharePoint", ProjectDescription: "SharePoint 2013", ProjectManager: "Test0" },
        { Title: "Lync", ProjectDescription: "Lync 2013", ProjectManager: "Test1" },
        { Title: "Exchange", ProjectDescription: "Exchange 2013", ProjectManager: "Test2" }
    function insertListItems(items) {
        for (var item in items) {
            var inputData = new Object();
            var type = new Object();
            type.type = getListItemType(listName);
            inputData.__metadata = type;
            for (var prop in items[item]) {
                inputData[prop] = items[item][prop];
            var obj = {
                url: url,
                type: "POST",
                contentType: "application/json; odata=verbose",
                data: JSON.stringify(inputData),
                headers: { "Accept": "application/json; odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val() },
                success: function (successData) { console.log("insert successful"); },
                error: function (errorData) { console.log("insert failed") }
            if (parent) {
                var exec = new SP.RequestExecutor(appWebUrl);
            } else {

    when I run it it prints

    {"__metadata":{"type":"SP.Data.ProjectsListItem"},"Title":"SharePoint","ProjectDescription":"SharePoint 2013","ProjectManager":"Test0"}'Projects')/items?@target=''

    {"__metadata":{"type":"SP.Data.ProjectsListItem"},"Title":"Lync","ProjectDescription":"Lync 2013","ProjectManager":"Test1"}'Projects')/items?@target=''

    {"__metadata":{"type":"SP.Data.ProjectsListItem"},"Title":"Exchange","ProjectDescription":"Exchange 2013","ProjectManager":"Test2"}'Projects')/items?@target=''

    insert successful insert successful insert successful

    I have multiple problems with this source code

    1. No records actually get inserted. but no errors are thrown.

    2. How do I ensure that in my main method... the code which comes after the insertListItems method is only executed when all the 3 records have been inserted successfully. currently because of the async nature of javascript the flow moves forward even before anything has been inserted.

    3. I googled on option 2 and found that jquery has a promise and deffered object pattern but it seems SP.RequestExecutor.executeAsync does not follow that and throws an error on the "done" method saying no such method is supported.

    This is how my list looks

    val it: unit=()

    Saturday, July 20, 2013 11:01 PM


All replies