SharePoint应用程序使用REST API在主机Web列表中插入记录

时间:2013-07-20 22:47:07

标签: javascript sharepoint-2013 sharepoint-apps

我编写了以下代码,以便在主机站点中包含的列表中插入记录。

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);

当我运行它时会打印

{“__ metadata”:{“type”:“SP.Data.ProjectsListItem”},“标题”:“SharePoint”,“ProjectDescription”:“SharePoint 2013”​​,“ProjectManager”:“Test0”}

http://app-e9f4c136adfd88.abhiapps.com/SPAppHelper/_api/SP.AppContextSite(@target)/web/lists/getbytitle('项目')/项目?@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('项目')/项目?@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('项目')/项目?@target ='http://dev.abhi.com'

插入成功  插入成功  插入成功

我对此源代码存在多个问题

  1. 实际上没有插入任何记录。但不会抛出任何错误。

  2. 如何确保在我的main方法中... insertListItems方法之后的代码仅在成功插入所有3条记录时执行。目前由于javascript的异步特性,即使在插入任何内容之前,流程也会向前移动。

  3. 我搜索了选项2,发现jquery有一个promise和deffered对象模式,但似乎SP.RequestExecutor.executeAsync不遵循这一点并在“done”方法上抛出错误,说不支持这样的方法

  4. 这就是我的列表的外观

    enter image description here

1 个答案:

答案 0 :(得分:0)

用这个替换你的obj:

        ....
        method: "POST",
        body: JSON.stringify(inputData),
        headers: {
                 "Accept": "application/json;odata=verbose",
                 "content-type": "application/json;odata=verbose",
                 "X-RequestDigest": $("#__REQUESTDIGEST").val()
             }
        ....
        ....