Вопрос

I am trying to get the newest item added to a list.

Get item by ID works for single item:

$.ajax({
url: xurl + "/_api/web/lists('" + xlist + "')/items/getById(123)", 
type: "GET", headers: { "Accept": "application/json;odata=verbose", },
  success: function (data) {
    var xid = data.d.ID;
    alert(xid);},
  error: function (error) { alert(JSON.stringify(error)); }
});

Get top item, doesn't work, it returns undefined:

$.ajax({
url: xurl + "/_api/web/lists('" + xlist + "')/items?$top=1&$orderby=ID desc", 
type: "GET", headers: { "Accept": "application/json;odata=verbose", },
  success: function (data) {
    var xid = data.d.ID;
    alert(xid);},
  error: function (error) { alert(JSON.stringify(error)); }
});

Suggestions?

Это было полезно?

Решение

The URL and endpoint seems right.

But in success, you will get the return data in data.d.results array.

So you can extract the ID of item from data.d.results array.

In this case, You will not get the ID of item directly using data.d.ID.

Try adding below code in success function:

if(data.d.results.length > 0) {
    var xid = data.d.results[0].ID;
    console.log(xid);
} 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top