문제

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