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) {
},
});
};
Is there a way to get the next item ID which handles deleted items too? E.g. If you have 10 items, but delete item#10, if you run this code it will get the highest item ID (9), add 1 to give you a next ID of 10 - however if you actually add an item it will be item ID 11 because 10 was deleted...
ReplyDeleteNo, You cannot recreate an ID. If you want it to be sequential, then you need to export the list item to a new list.
ReplyDeleteBut Suppose you had deleted all items from list then what happens in this case?
ReplyDelete