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

I am trying to get item from SharePoint list by passing a parameter with the ID to the list and retrieving the item. My code is this:

$.ajax({
url: 'http://mysite:5555/_api/web/lists/getbytitle("my list")/items/getbyid(1)',
header: {"Accept": "application/json;odata=verbose"},
success: function(data){ 
Var jsonObject= JSON.parse(data.body);
alert(jsonObject.d.Title);}  });

the file returned is an xml file with a long amount of data, I have an error that I could trace stating: unable to get property d of undefined or null reference

Any hint?

share|improve this question
    
The big V in Var is a typo i guess? Take a look with for example fiddler to se how the response you are getting is actually looking –  Robert Lindgren Jul 3 '13 at 5:16
    
I tried that, If I take the url in fiddler and put it in the address bar, I could see the document, but my problem is parsing the document, I have an error when I try to read the item –  Richard Jul 4 '13 at 4:27
    
I guess one thing to ensure is that that that you have an Item with ID 1 and that the list is called "my list". Otherwise your code looks correct. I would look at the JSON head data in the Chrome developer tools, that should show you what your trying to display. –  Stephen Feb 18 at 12:57
    
Any resolution on this? –  Robert Kaucher Apr 1 at 19:24

3 Answers 3

There is no need to parse the data the way you are. A get by ID returns a single object in JSON format.

$.ajax({
    url: "https://mysharepoint.com/_api/lists/getbytitle('Test List')/items/getbyid(3)",
    headers: { 'accept': 'application/json;odata=verbose', 'content-type': 'application/json;odata=verbose'},
    success: function(data){ 
       alert(data.d.Title);
}  });

That will return a request body that looks like this:

{
    "d": {
        "__metadata": {
            "id": "f02d2168-0e8c-40ec-8222-7b97377e550b",
            "uri": "https://sharepoint.domain.com/_api/Web/Lists(guid'efd16a43-aaf4-44fd-a9a1-4634afe328ac')/Items(3)",
            "etag": "\"1\"",
            "type": "SP.Data.Test_x0020_ListListItem"
        },
        "Title": "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore ",
        "Test_x0020_Item": "Enter Choice #1",
        "DueDate": null,
        "DateCompleted": null,
        "StatusId": 3,
        "OtherInfo": null,
        "ID": 3,
        "Modified": "2014-02-28T02:11:22Z",
        "Created": "2014-02-28T02:11:22Z",
        "AuthorId": 12,
        "EditorId": 12,
        "OData__UIVersionString": "1.0",
        "Attachments": false,
        "GUID": "4769a6db-3a22-4c53-922f-0d6744f82fb0"
    }
}
share|improve this answer

Can you try appending Title in the url like this:

url: 'http://mysite:5555/_api/web/lists/getbytitle("my list")/items/getbyid(1)?$select=Title'
share|improve this answer
    
Thanks Nadeem, Same issue, I opened the request in Fiddler, and copied the GET requested, pasted it in URL in Firefox, and could get the file, but through JavaScript I am not able to retrieve it properly –  Richard Jul 3 '13 at 3:55
    
Are you sure about the name of the field. For example I have seen in the past in SharePoint 2010 that REST uses display name of the field minus any spaces and the field names are also case sensitive. See this sharepointnadeem.blogspot.in/2012/01/… –  Nadeem Yousuf Jul 3 '13 at 4:55
    
I am trying to retrieve the column field that comes by default from SharePoint. Isn't that the case? –  Richard Jul 3 '13 at 11:23
    
Just thought you may have changed the name of the field. –  Nadeem Yousuf Jul 3 '13 at 11:33
    
ah, no, what should I do in that case? –  Richard Jul 3 '13 at 11:34

Try this:

$.ajax({
url: 'http://mysite:5555/_api/web/lists/getbytitle("my list")/items/getbyid(1)/?$select=Title',
header: {"Accept": "application/json;odata=verbose"},
success: function(data){ 
alert(data.d.results.Title);}  });

It is hard to help you without knowing what your results look like. What does the JSON data look like expanded in your debugging window?

share|improve this answer

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.