jQuery OData substringof or startswith 返回所有项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16100697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:26:21  来源:igfitidea点击:

OData substringof or startswith returning all items

restjqueryodatasharepoint-2013

提问by Mark

I'm trying to filter my results from a Rest Call.

我正在尝试从 Rest Call 中过滤我的结果。

$.ajax({
    type: "GET",
    headers: {
        "Accept": "application/json;odata=verbose"
    },
    dataType: "JSON",
    url: _spPageContextInfo.webServerRelativeUrl + "/_api/lists/getByTitle('Contacts')/items?$select=Title,Id&$startswith('Title','" + request.term + "') eq true",
    success: function (data) {
    },
    error: function (ex) {
    }
});

In my Contacts List i'm trying to retrieve the Title and the Id for Items which start with a String or which have the String somewhere in it, here for example it is the Name of somebody.

在我的联系人列表中,我试图检索以字符串开头或其中某处有字符串的项目的标题和 Id,例如,它是某人的姓名。

I also tried it with substringof:

我也用 substringof 尝试过:

"/_api/lists/getByTitle('Contacts')/items?$select=Title,Id&$substringof(" + request.term + ",'Title') eq true"

which delivers also the same result.

这也提供了相同的结果。

It gives me all List Items from the List and no Filtering is applied. I build the Url for the Rest after looking here Programming using the SharePoint 2013 REST serviceLike the Schema given there I think the Url looks ok, but it not seems so :)

它为我提供了列表中的所有列表项,并且没有应用过滤。在查看此处之后,我为其余部分构建了 Url使用 SharePoint 2013 REST 服务进行编程就像那里给出的架构一样,我认为 Url 看起来不错,但似乎并非如此:)

Edit:

编辑:

Applying the $filterlike in the OData Uri Conventions gives me the following error:

$filter在 OData Uri 约定中应用此类会给我以下错误:

{"error":{"code":"-1, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The query is not valid."}}}

Tried it with following Query Strings:

使用以下查询字符串进行了尝试:

_api/lists/getByTitle('Contacts')/items?$select=Title,Id&$filter=substringof(m,'Title') eq true

_api/lists/getByTitle('Contacts')/items?$select=Title,Id&$filter=substringof('m','Title') eq true

_api/lists/getByTitle('Contacts')/items?$select=Title,Id&$filter=substringof('m',Title) eq true

回答by nersofiripi

I've managed to get the filter with substringof returning the correct results when I removed the "eq true".

当我删除“eq true”时,我设法得到了带有 substringof 的过滤器返回正确的结果。

Using one of your query strings, it should work like this:

使用您的查询字符串之一,它应该像这样工作:

_api/lists/getByTitle('Contacts')/items?$select=Title,Id&$filter=substringof('m',Title)

I haven't checked any other functions, but at least, the same happens with startswith function.

我没有检查任何其他函数,但至少,startswith 函数也是如此。

回答by Keith Hudson

For anyone looking at this question, I can report that

对于任何看这个问题的人,我可以报告

/_api/web/lists/GetByTitle('Applications')/items?$filter=startswith(Title,'1AAJ') 

IS working for me.

正在为我工​​作。

回答by Rolfvm

Check http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/for the correct uri convention.

检查http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/以获取正确的 uri 约定。

Should be

应该

/_api/lists/getByTitle('Contacts')     
/items?$select=Title,Id&$filter=substringof(" + request.term + ",'Title') eq true"

So with the $filter included

所以包含 $filter

回答by Robesz

I tried your query URI on my endpoint and applied some changes: - The second parameter of the substring shouldn't be a string, so I removed the apostropes

我在端点上尝试了您的查询 URI 并应用了一些更改: - 子字符串的第二个参数不应是字符串,因此我删除了撇号

After this I get the results:

在此之后,我得到了结果:

http://jaydata.org/examples/Northwind.svc/Products?$select=Product_ID,Product_Name&$filter=substringof('CH',Product_Name)

http://jaydata.org/examples/Northwind.svc/Products?$select=Product_ID,Product_Name&$filter=substringof('CH',Product_Name)

My endpoint is standard WCF Data Service, and the filter is working.

我的端点是标准的 WCF 数据服务,过滤器正在工作。

If changing the URI still returns all records, that would be a SherePoint trick I guess. What happens if you put 'zzz' or some random string in the filter?

如果更改 URI 仍然返回所有记录,那将是我猜的 SherePoint 技巧。如果在过滤器中放入 'zzz' 或一些随机字符串会发生什么?

回答by lyndon hughey

Also, the contains method works and I've had better compatibility with it. The syntax is:

此外, contains 方法有效,我与它有更好的兼容性。语法是:

api/People?$filter=contains(LastName,%27Smith%27)&$select=LastName,FirstName