3

I have created the list in SharePoint 2013. I got the list item id using javascript. But the recently deleted item id is not updated in SP-2013. I need to get the upcoming list item id. Please suggest an idea.

For example, if list having 20 items. The last item id is 20. I have deleted the last two items. Now the list having last id of 18. But newly inserting item id is 21. so, how to get the upcoming item id using javascript.

Note : the list item inserting in SharePoint itself, not custom. Please suggest an idea.

1

One option is to create ItemAdded (-ing) event receiver and write into the list property bag ID of the newly created item.

By this way you can always have latest id regardless of the deleted items.

1

You can get through rest API using the following URL:

http://www.yourdomainname.com/_api/web/lists/getByTitle('ListName')/items?$select=ID&$orderby=ID desc&$top=1

You can also make a call to get this value through jQuery ajax:

var url = "/_api/web/lists/getByTitle('ListName')/items?$select=ID&$orderby=ID desc&$top=1"

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + url,
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {
        console.log(data.d.results[0].ID)
    },
    error: function (error) {
        console.log(error);
    }
});

The ID of the last item of your list i'll be retrieved in data.

  • 3
    It won't give actual last ID, suppose if last item is deleted.. – Ronak Patel Dec 20 '16 at 4:14
0

I was looking for the same thing. My solution:

  • I created another list that is only used to store the ID from the first list
  • Using a workflow triggered on creation on the first list, it adds the ID in the second list in a dedicated column
  • Using JavaScript I check the second list to get the last ID created from the first list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.