Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
    
u mean to say highest ID value? –  Vishal_Sharepoint Aug 17 '12 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 '12 at 19:14

3 Answers 3

up vote 3 down vote accepted

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();
query.set_viewXml(caml);
var items = list.getItems(query);
ctx.load(items)
ctx.executeQueryAsync(function() {
    var enumerator = items.getEnumerator();
    enumerator.moveNext();
    var item = enumerator.get_current();
    var id = item.get_id();
    alert(id);
}, function() { 
    //failure handling comes here
    alert("failed"); 
});

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
        alert(id); 
    }).fail(function() {
        alert("hmpf");
    });
share|improve this answer
    
@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 '12 at 19:10
    
also, is the FSObj type what ignores the folder? –  Justin Aug 17 '12 at 19:16
    
Exactly! FSObj = 1 is folder, FSObj = 0 is everything else. –  Anatoly Mironov Aug 26 '12 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. –  Anatoly Mironov Aug 26 '12 at 12:21

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

var soapEnd = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Body>
    <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>
        <viewFields>
            <ViewFields>
                <FieldRef Name='ID' />
            </ViewFields>
        </viewFields>
        <rowLimit>1</rowLimit>      
    </GetListItems>
</soap:Body>

";

then ajax call -

$.ajax({
    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) {
$(xData.responseXML).find("z\\:row").attr("ows_ID")}

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

share|improve this answer
    
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 '12 at 7:25
    
had no idea about this one! thanks very much! –  Justin Aug 17 '12 at 19:12

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

share|improve this answer
    
$().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). –  Anatoly Mironov Aug 17 '12 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 '12 at 19:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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