SharePoint 2013 App - CRUD操作

时间:2013-07-31 05:14:11

标签: c# sql sharepoint

我不熟悉使用外部内容的Sharepoint 2013应用。我可以从SQL中提取数据,并可以在App页面中填充。但是,我尝试添加新数据,我收到以下错误消息


来自网页的消息

{"readyState":4,"responseText":"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"An entry without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified.\"}}}","status":400,"statusText":"Bad Request"}

确定

我使用了以下代码

create: function (dname, description) {
    debugger;
    $.ajax({
        url: _spPageContextInfo.webServerRelativeUrl +
                    "/_api/web/lists/getByTitle('tblDomain_SP')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify({'DomainName':dname,'Description':description}),
        dataType: "json",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function () {
            REST.DomainFormFiller.clear();
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }

    });

}
你可以帮我把数据插入sql表吗?

1 个答案:

答案 0 :(得分:0)

错误:

  

找到了没有类型名称的条目,但没有预期的类型   指定。允许没有类型信息的条目,预期   在指定模型时也必须指定type

由于List Item条目中缺少Type属性而发生。

列出项目类型

要创建列表项,您需要包含列表项类型。这是首次创建列表时由SharePoint自动创建的字符串。获得这个的两种可能方法是:

  • 对列表执行读取操作并找到其中的类型 返回的元素集。在JSON格式中,可以在其中找到类型 所有返回列表项的__metadata属性。
  • 您可以尝试从列表名称生成值。一般来说 列表项类型遵循约定SP.Data.ListNameListItem(例如 列表名称为Test,列表项类型为SP.Data.TestListItem)。然而 这并非总是如此。例如SharePoint自动 大写列表名称的第一个字母(例如列表名称test 列表项类型为SP.Data.TestListItem)。

解决方案

以下代码段显示了如何根据列表名称生成列表项类型:

function GetItemTypeForListName(name) {
    return"SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}

然后你需要更换一行:

data: JSON.stringify({'DomainName':dname,'Description':description}),

这一个:

data: JSON.stringify({'DomainName':dname,'Description':description,"__metadata": { "type": GetItemTypeForListName('tblDomain_SP') }}), 

参考

Manipulating list items in SharePoint Hosted Apps using the REST API