Is there a way to get the last ID from a Document Library with javascript/jquery?

  • u mean to say highest ID value? Aug 17, 2012 at 6:41
  • didn't really ask this properly...apologies. I meant the item that was most recently created, which I suppose most likely is the highest ID value. My bad.
    – Justin
    Aug 17, 2012 at 19:14

3 Answers 3


There was a similar question in SharePoint Stackexchange. I even wrote a blog post about retrieving the last item in a list/library: Three ways to get the id of last created SPListItem. There I show the last item for current user... If you want to retrieve the very last document, it is much easier. I prefer the Client object model. But it can be done with SPServices and listdata.svc. Here is the code:

var caml = "<View><Query><Where>"
    + "<Eq><FieldRef Name='FSObjType' /><Value Type='int'>0</Value></Eq>"
    + "</Where>" 
    + "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>" 
    + "</Query>"
    + "<ViewFields><FieldRef Name='ID' /></ViewFields>"
    + "<RowLimit>1</RowLimit>"
    + "</View>";
var ctx = SP.ClientContext.get_current()
var web = ctx.get_web();
var list = web.get_lists().getByTitle("Shared Documents")
var query = new SP.CamlQuery();
var items = list.getItems(query);
ctx.executeQueryAsync(function() {
    var enumerator = items.getEnumerator();
    var item = enumerator.get_current();
    var id = item.get_id();
}, function() { 
    //failure handling comes here

It is important to ignore folders, otherwise you'll get a latest folder, not the latest document.

If OData is preferred (listdata.svc) you can use it as well:

jQuery.getJSON("/_vti_bin/listdata.svc/Documents()?$orderby=Id desc&$top=1&$select=Id")
    .done(function(data) {
        var id = data.d[0].Id
    }).fail(function() {
  • @Anatoly...thanks for the example and all the info. I was using SPServices for a bit and ran into the problem you stated below. Was wondering about the other methods. Out of curiousity how does the OData work? How do you extract the data from that? Could I use jQuery? Thanks again!
    – Justin
    Aug 17, 2012 at 19:10
  • also, is the FSObj type what ignores the folder?
    – Justin
    Aug 17, 2012 at 19:16
  • Exactly! FSObj = 1 is folder, FSObj = 0 is everything else. Aug 26, 2012 at 12:13
  • @Justin odata is a good approach. If you specify json as datatype in jQuery.ajax you'll get it in json which are more or less ready objects to use in javascript. The biggest problem with OData according to me are the displaynames that are used as property names (this causes problems in sites with 2 or more languages). Luc Stakenborg lists all other issues in his blog. Aug 26, 2012 at 12:21

SPServices has a function called GetLastItemId you can use. Here is the documentation for that function: $().SPservices.GetLastItemId

  • $().SPservices.GetLastItemId is very helpful, indeed. But you can only get the last item id, modified by a specific user (current user, if not specified). Aug 17, 2012 at 12:50
  • yeah this is totally my fault...apologies. I have been using SPServices method above an ran into the problem Anatoly mentioned. But my question was bad. This is a good answer for the question.
    – Justin
    Aug 17, 2012 at 19:11

I think you need some thing like this - Create the soap envelope

var soapEnd = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
        <listName>Your Doc Library Name</listName>
        <query><Query><OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy></Query></query>
                <FieldRef Name='ID' />


then ajax call -

    url: "/_vti_bin/lists.asmx",
    type: "POST",
    dataType: "xml",
    data: soapEnv,
    complete: ProcessListItems,
    contentType: "text/xml; charset=\"utf-8\""

and in the ProcessListItems you can easily retrieve the value

function ProcessListItems(xData, status) {

or alternatively you can simply bring all rows all columns, get the first one and retrieve value, in that case ommit viewFields and rowLimit

I assumed you meant last modified by 'Last' ID, in case anything else please edit the query portion

Please note this is not a tested code, this is just one sample written instantly, please re-write this as per ur requirement

  • it is an id of the last item, but not last id of document library. There are exists situation when the last item are deleted. In this case the last item id and last possible list id are different.
    – Alexander
    Aug 17, 2012 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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