4

How can I retrieve via REST service in SharePoint item column values in JSON:

This is my snippet:

$.ajax({
  url: '/_api/web/lists/getbytitle("my list")/items/getbyid(3)',
  type: 'GET',
  header: {'Accept': 'application/json;odata=verbose'},
  success: function(data) { alert(data.title); }
});

It's not working, any help?

1
  • 2
    You really need to put some more context into your questions - from that snippet its really not clear how you are calling the services. We can guess, but might be wrong so include a little more detail
    – Ryan
    Jul 2, 2013 at 15:03

4 Answers 4

7
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>   
<script>
 function restEndPoints2013(){

$.ajax({ 
    url: "http://site/_api/Web/Lists/GetByTitle('listName')/items(3)", 
    type: "GET",
    headers: {"accept": "application/json;odata=verbose"},
    success: function(data){
        $.each(data.d.results, function(index, item){  
                  alert(item.Title);
        });
    },
    error: function(error){
        alert(JSON.stringify(error));
    }
});
}
</script>
<body>
<script>
restEndPoints2013();
</script>

Dump this into an HTML editor and upload it to a server and see what happens.

3

Iterate through data.d.results:

function(data) {
$.each(data.d.results, function(i,result) {
//get item property
var strTitle = result.Title;
});
2
  • Thanks Nadeem, I tried it and I got the error in JavaScript that it can't get the property results for the undefined data.d It can't know what is d in the data. Jul 2, 2013 at 22:58
  • any more ideas? Jul 2, 2013 at 23:18
2

You can easily use jQuery for this. At first you build your Query String:

//For the Element with the ID 1
var urlQuery = "https://www.url.i/site/_vti_bin/ListData.svc/Listname?$filter=ID eq 1"; 

Then you can access the items from the list:

 $.getJSON(urlQuery,
    function (data) {
       $.each(data.d.results, function (i, item) {
          var title = item.Title;
       });
    });

EDIT 1: Fixed it, should work for SP2010.

EDIT 2: For SP 2013 you can use this Query String:

http://url.i/sites/siteName/_api/web/lists/getByTitle('listName')/items?$filter=Id eq 1

Then you should get the element as XML. If you put

accept: application/json;odata=verbose

in the HTTP-Header you get the result as JSON

Here are some more information about Rest in SharePoint 2013

4
  • Thanks Mbauer, but I need to pass a parameter to the list with the ID of the item and get the title, so what am using is something like this: $.ajax({ url: '/_api/web/lists/getbytitle("my list")/items/getbyid(3), type: "GET", header: {"Accept": "application/json;odata=verbose"}, success: function(data){ alert(data.title);}, but it's not working Jul 2, 2013 at 22:44
  • the data returned in xml not json..! Jul 2, 2013 at 23:49
  • what SharePoint version are you using?
    – mbauer
    Jul 3, 2013 at 5:36
  • Foundation 2013 Jul 3, 2013 at 17:09
0

may be you should use $.ajax instead of $.getJSON and replace "header: {"Accept": "application/json;odata=verbose"}" by " dataType: 'json' " in order to get the response in JSON format. Besides, try to add to your ajax the parameter "async: false" to make the request synchronous because you are trying to get the value from an empty object.

1
  • Won't work very well against SharePoint REST API without the header proposed by mbauer! May 8, 2014 at 18:53

Your Answer

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

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