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.

6

3 Answers 3

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.

2
  • thanks, it worked. _vti_bin is working in web browser that confused me
    – Vishnu S
    Commented May 18, 2017 at 9:35
  • 1
    It is supposed to work but the syntax is different. See the jQuery / SPServices Library, that uses the SOAP endpoint and sometimes still has greater powers than REST Commented May 18, 2017 at 9:37
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));
    }
});
}
2
  • I tried that one, but getting error
    – Vishnu S
    Commented May 18, 2017 at 9: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
    Commented May 18, 2017 at 9: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",
    }
  });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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