0

I need to get the no.of items in a SharePoint list which satisfying a condition.

https://<SITEURL>/_vti_bin/listdata.svc/LISTNAME/$count?&$filter=FIELDNAME eq 'abc'


using above url in browser gives me correct result.

So I Tried get it using below code,

    getItems("/_vti_bin/listdata.svc/LISTNAME/$count?&$filter=FIELDNAME eq 'abc'").done(function(data){
                console.log(data);
            });

getitems function,

function getItems(url){

    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        }
    });
}


But using this code gives an error "415 Unsupported Media Type".
please help.

Vishnu S
  • 864
  • 1
  • 14
  • 25

3 Answers3

2
/_vti_bin/listdata.svc/LISTNAME/$count?&$filter=FIELDNAME eq 'abc'

_vti_bin is the old (SP2007) SOAP WebServices endpoint (might still work),
But it is not the same as the Modern (SP2010) _api REST end point:

/_api/web/lists/getbytitle('LISTNAME')/items?$filter=FIELDNAME eq 'abc' 

You will get the resultst back in data.d.results, you can get the length of.

Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79
0

Instead of writing the result in done function you can use success and error methods. Your code look like

function getItems(url){

return $.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + url,
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    }
 success: function (data) {
        console.log(data.d.results);
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});
}
Jussi Palo
  • 8,196
  • 1
  • 20
  • 36
Mukesh M
  • 49
  • 3
  • I tried that one, but getting error – Vishnu S May 18 '17 at 09:04
  • {"readyState":4,"responseText":"{\r\n\"error\": {\r\n\"code\": \"\", \"message\": {\r\n\"lang\": \"en-US\", \"value\": \"Unsupported media type requested.\"\r\n}\r\n}\r\n}","status":415,"statusText":"Unsupported Media Type"} – Vishnu S May 18 '17 at 09:08
0

You should set the "Accept" header to "text/plain" when querying for counts from "/_vti_bin/" URL via ajax.

function getItems(url){

return $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + url, type: "GET", headers: { "accept": "text/plain", } }); }