0

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  
           }  
    });  
}      
1

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
  • Thanks Sunil for the quick reply but even if use this simple query :- var caml = "<View><Query><Where><Eq><FieldRef Name='PublishedMonth'/><Value Type='Number'>11</Value></Eq></Where></Query></View>"; it doesn't work – naijacoder Nov 7 '19 at 6:55
  • can you share your data list view for better understanding – Sunil Sahu Nov 7 '19 at 7:02
  • What do you mean? If i'm using rest this would be my URL: _api/web/lists/GetByTitle('Posts')/items?$select=id,Title,News_x0020_Images,PublishedDate,Byline. As i said its a OTB Posts list – naijacoder Nov 7 '19 at 7:05
  • Sunil ..I changed the contains to eq and changed items to GetItems and nothing worked. – naijacoder Nov 7 '19 at 23:23
  • got it and it works changed these three things type: "POST" data: JSON.stringify(data), and from Value Type='Number' to Type=Calculated – naijacoder Nov 8 '19 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.