getByTitle() is not returning Sharepoint List



我正在尝试使用ajax调用从Jquery中获取几个SharePoint(2013(列表,如下所示

$.ajax({
url: url + "/_api/web/lists/getbytitle('" + listname + "')/items?$top="+maxReturnedRows,
method: "GET",
async: false,
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
        listArray = data.d.results;         
    },
error: function (data) {
    alert('Error in AJAX-Call to '+listname);
    failure(data);
}

}(;

其中 url 包含 SharePoint 网站的 URL。这个电话给了我下面的错误,

错误代码:-1,System.Collections.Generic.KeyNotFoundException错误消息:字典中不存在给定的键。

JSON 响应:-

"{"error":{"code":"-1, System.Collections.Generic.KeyNotFoundException","message":{"lang":"de-DE","value":"The given key was not present in the dictionary."}}}"

我尝试使用以下 URL 在浏览器中调试该问题

 <baseurl>+ "/_api/web/lists/getbytitle('" + listname + "')

我收到了带有列表 Metada 的 XML 响应,但如果我尝试使用以下 URL 获取该列表的项目,

<baseurl> + "/_api/web/lists/getbytitle('" + listname + "')/Items

收到 500 错误,我怀疑是因为同样的问题。

作为另一种选择,尝试使用"guid"获取相同的列表,如下所示,

    <baseurl>/_api/Web/Lists(guid'xxxxxxxxxxxxxxx')

如果我尝试访问此列表中的项目,它正在工作,我收到错误 500

<baseurl>/_api/Web/Lists(guid'xxxxxxxxxxxxxxx')/Iems

请让我知道,如何解决此问题。

提前致谢

列表有两个"名称"。首次通过 UI 创建时,输入的名称将用作根 URL 和标题。然后,您可以更改列表的标题,但 URL 保持不变。

下面是PowerShell中重命名的列表的属性,该列表创建为"Cars2",然后重命名:

PS C:> $calcdemo.lists["Cars for sale"] | select title,rootfolder
Title         RootFolder
-----         ----------
Cars For Sale Lists/Cars2

由于 REST "getByTitle" 使用 title 属性而不是 URL,因此在示例代码中,"listname" 变量需要是"待售汽车",而不是"Cars2"。

此外,如果列表标题包含 Unicode 字符,则可能需要对这些字符进行转义。

url: url + "/_api/web/lists/getbytitle('" + listname + "')/items?$top="+maxReturnedRows,

应按 GUID 获取列表项:

例如:

 /_api/web/lists('%7B326038AE-D47E-454D-AAD4-5440E6133EF2%7D')

最新更新