SharePoint 2013: Get next item ID from a SharePoint Document Library using REST
Environment: Using jQuery, REST, SharePoint 2013 Enterprise, knockout JS
Requirement: To get the next item ID from a SharePoint Document Library using REST
Prerequisite: knockout and latest jQuery are defined in the code.
Code:
function getNextSequenceID() {
var documentLibraryName = 'DocumentLibraryName';
var serverUrl = window.top._spPageContextInfo.webServerRelativeUrl;
//Query the top 1 item sorted in descending order.
var fileListItemIDUri = serverUrl + "/_api/web/lists/getbytitle('" + documentLibraryName + "')/items?$select=ID&$top=1&$orderby=ID%20desc";
var sequenceID = 1;
return $.ajax({
url: fileListItemIDUri,
type: "GET",
headers: { "accept": "application/json;odata=verbose" },
success: function (data) {
if(data.d.results.length==1)
{
var id = data.d.results[0].ID;
sequenceID = id+1;
}
alert(“The next item ID is :”+ sequenceID);
},
error: function (data) {
},
});
};