Using NewForm.aspx
If you are using the default new form to add items to the list, add the following javascript on the page you're redirecting to.
function getLastItemID(){
var queryBuffer = [];
queryBuffer.push("<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='User'>"+_spUserId+"</Value></Eq></Where>");
queryBuffer.push("<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>");
var res = spjs_QueryItems({listName:thisListGuid,query:queryBuffer.join(''),viewFields:['ID'],rowLimit:1});
if(res.count<0){
alert("An error occurred. Most likely the parameter \"thisListGuid\" is wrong.");
}
else if(res.count>0){
return res.items[0].ID;
}else{
return '';
}
}
Usage:
var id= getLastItemID();
if(id)
fn_appendStatusBar(id);
Note:
1. Replace thislistGuid
with the listName or GUID.
2. You need to download spjs-utility.js and refer it in your javascript. You can find it here: spjs-utility.js
Source: spjsblog
Using Custom Form
If you are using a custom form, you can get the ID when you're adding the item to the list:
var clientContext = SP.ClientContext.get_current();
var webSite = clientContext.get_web();
var lists = webSite.get_lists();
var testList = lists.getByTitle("TestList");
var itemCreationInfo = new SP.ListItemCreationInformation();
var listItem = testList.addItem(itemCreationInfo);
listItem.set_item("bla", "bla");
listItem.update();
clientContext.executeQueryAsync(Function.createDelegate(this,this.Succeeded),Function.createDelegate(this,this.Failed));
function onQuerySucceeded() {
fn_appendStatusBar(listItem.get_id());
}