Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
1  
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 '13 at 15:03

4 Answers 4

<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.

share|improve this answer

Iterate through data.d.results:

function(data) {
$.each(data.d.results, function(i,result) {
//get item property
var strTitle = result.Title;
});
share|improve this answer
    
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. –  Richard Jul 2 '13 at 22:58
    
any more ideas? –  Richard Jul 2 '13 at 23:18

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

share|improve this answer
    
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 –  Richard Jul 2 '13 at 22:44
    
the data returned in xml not json..! –  Richard Jul 2 '13 at 23:49
    
what SharePoint version are you using? –  mbauer Jul 3 '13 at 5:36
    
Foundation 2013 –  Richard Jul 3 '13 at 17:09

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.

share|improve this answer
    
Won't work very well against SharePoint REST API without the header proposed by mbauer! –  Robert Lindgren May 8 at 18:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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