The method that i am using to get the item id for the previous item in a list is by using the Javscript Client object model. This is done by retriving all the items in the list and then getting the last one to get it's id. The items are retrived in descending order (by Created) and the first one if picked as the last item added.
Note - This does not always retun correct results. It works fine if no item is deleted form the list.
Code -
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getOldItemid, "sp.js");
function getOldItemid()
{
context = new SP.ClientContext.get_current();
web = context.get_web();
var list = web.get_lists().getByTitle(‘yourlistName’);
var camlQuery = new SP.CamlQuery();
var _query =”<view><query><where></Where></Query><orderby><fieldref Name='Created' Ascending=’FALSE’/></OrderBy></View>";
camlQuery.set_viewXml(_query);
this.listItems = list.getItems(camlQuery);
context.load(listItems, ‘Include(Id,Created)’);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),Function.createDelegate(this, this.onFailureMethod));
}
function onSuccessMethod(sender, args)
{
var count = parseInt(this.listItems.get_count());
if(count >0)
{
var currentItem = listItems[0]; //-> get the first item form Desc order of items
var _ID = currentItem.get_item("Id");// get id of this item.
}
}
function onFaiureMethod(sender, args)
{
}
</script>
Big Thanks to this post for pointers.
No comments:
Post a Comment