none
Insert List Item in a parent list by using REST API

    Question

  • 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];
            }
    
            console.log(JSON.stringify(inputData));
            console.log(url);
    
            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);
                exec.executeAsync(obj);
    
            } else {
                $.ajax(obj);
            }            
        }
    }
    
    insertListItems(projectItems);

    when I run it it prints

    {"__metadata":{"type":"SP.Data.ProjectsListItem"},"Title":"SharePoint","ProjectDescription":"SharePoint 2013","ProjectManager":"Test0"}

    http://app-e9f4c136adfd88.abhiapps.com/SPAppHelper/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Projects')/items?@target='http://dev.abhi.com'

    {"__metadata":{"type":"SP.Data.ProjectsListItem"},"Title":"Lync","ProjectDescription":"Lync 2013","ProjectManager":"Test1"}

    http://app-e9f4c136adfd88.abhiapps.com/SPAppHelper/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Projects')/items?@target='http://dev.abhi.com'

    {"__metadata":{"type":"SP.Data.ProjectsListItem"},"Title":"Exchange","ProjectDescription":"Exchange 2013","ProjectManager":"Test2"}

    http://app-e9f4c136adfd88.abhiapps.com/SPAppHelper/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Projects')/items?@target='http://dev.abhi.com'

    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

Answers

All replies