使用SharePoint 2013 REST API添加列表项

25

我正在尝试使用SharePoint 2013和REST API在现有列表中添加新项。

这里有非常好的文档:http://msdn.microsoft.com/en-us/library/jj164022(office.15).aspx#ListItems

我要添加项的列表名为“Resources”,所以我执行以下HTTP POST操作来添加新项:

POST https://<site>/apps/reserve/_api/lists/getbytitle('Resources')/items
    X-RequestDigest: <digest_key>
    Content-Type: application/json;odata=verbose

    {
        "__metadata":    {"type": "SP.Data.ResourcesListItem"},
        "Title":         "New Title",
        "Description":   "New Description",
        "Location":      "Sunnyvale"
    }

但我收到了以下错误信息:

A type named 'SP.Data.ResourcesListItem' could not be resolved by the model.
When a model is available, each type name must resolve to a valid type.

所以我猜测我没有正确的名称来表示这个资源的名称。在文档中,它说:

To do this operation, you must know the ListItemEntityTypeFullName property of the list
and pass that as the value of type in the HTTP request body.

但我不知道如何获取我的列表的ListItemEntityTypeFullName,而且文档似乎没有解释如何做到这一点 - 我复制了文档中的模式(SP.Data.<LIST_NAME> ListItem"),但我想那不对。

我该如何找到我的列表名称?

1个回答

24

您可以按照以下方式获取名称:

GET https://<site>/apps/reserve/_api/lists/getbytitle('Resources')?$select=ListItemEntityTypeFullName

列表名称将在以下位置:content -> m:properties -> d:ListItemEntityTypeFullName


1
一旦您知道实体类型,有没有办法找出该类型的哪些属性是其中的一部分?我遇到了一个问题,即出现错误:“在类型'SP.Data.MyListListItem'上不存在属性'MyColumn'。请确保仅使用由该类型定义的属性名称。”MyColumn是MyList的默认ContentType的一部分。 - Jerzakie
@Jerzakie 很可能你正在使用列的显示名称而不是原始名称。你可以做两件事来找到原始名称:1)在“列表设置”下查找>悬停/选择该字段。URL 将有类似于 Field=SomeName 的内容。SomeName 是你想要的。2)如果你没有访问“列表设置”的权限,你可以转到 http://yoursite/_vti_bin/listdata.svc/ListName(SP 2010)或 http://yoursite/_api/Web/Lists/GetByTitle('ListName')(SP 2013+)。字段名称将在 <feed><entry><content><m:properties><d:SomeName>Your Display Name</d:SomeName> 下(在 2013+ 中省略 <feed>)。 - vapcguy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接