Is there a way to get the last ID from a Document Library with javascript/jquery?
3 Answers
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");
});
-
@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!– JustinAug 17, 2012 at 19:10
-
-
-
@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.– JustinAug 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/'>
<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
-
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. Aug 17, 2012 at 7:25