Help with using Caml in rest api calls - filtering doesn't work for me
-
08-02-2021 - |
Pergunta
I'm trying to filter a Posts list(Blog) by month and year. I created a calculated column and used the Published Date to extract month and year. But my below code doesn't filter correctly when i use rest api and caml below.
I am supposed to be returning no data or 1 item but its returning all items
I have tried all i can but no luck.
Not sure what i'm missing.
Thanks in advance
function restCallwithCaml(_urlblog,listName) {
// var caml = "<View><Query><Where><Eq><FieldRef Name='PublishedMonth'/><Value Type='Number'>11</Value></Eq></Where></Query></View>";
var caml ="<View>" +
"<Query>" +
"<Where>" +
"<And>" +
"<And>" +
"<Eq>" +
"<FieldRef Name='PublishedYear' />" +
"<Value Type='Number'>2019</Value>" +
"</Eq>" +
"<Eq>" +
"<FieldRef Name='PublishedMonth'/>" +
"<Value Type='Number'>11</Value>" +
"</Eq>" +
"</And>" +
"<Contains>" +
"<FieldRef Name='PostCategory'/>" +
"<Value Type='LookupMulti'>Events</Value>" +
"</Contains>" +
"</And>" +
"</Where>" +
"</Query>"
"</View>";
/// set request data
var data = { "query" :{"__metadata": { "type": "SP.CamlQuery" }, "ViewXml": caml}};
/// make an ajax call
$.ajax({
url: _urlblog + "/_api/web/lists/GetByTitle('"+ listName +"')/items",
method: "POST",
data: data,
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
'content-type': 'application/json;odata=verbose',
'accept': 'application/json;odata=verbose'
},
success: function (data) {
///do your code
console.log(data.d.results);
},
error: function (data) {
///do your code
}
});
}
Solução
You can replace your code snippet for query having contains with eq and think will resolve your your problem. As when filtering with multilookup column it is advised to use Eq instead of contains. For reference
Update
Also update the endpoint with
_api/web/lists/GetByTitle('"+ listName +"')/GetItems
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange