1

I would like to give user some notification with the ID of the item on status bar when a new item is added to a list, so far I got the javascript code for display text in status bar, but I don't know how to determine when the item is added to the list and how to get ID of the item.

What I want is something like this:

 <script>
 if(mylist.itemJustAdded ==true){
   fn_appendStatusBar(mylist.itemJustAdded.ID);
 }
 </script>

I already have fn_appendStatusBar(), but, how do I properly construct this if statement condition?

3
  • depends on where you are adding the new item, from a list webpart?
    – Mike
    Commented Jul 10, 2015 at 23:57
  • Are you adding a new item using list's oob form or you have made a custom form in which you have javascript code to add an item
    – Asad Refai
    Commented Jul 11, 2015 at 5:09
  • @Mike sorry for late response, I'm adding item from list webpart.
    – SSilicon
    Commented Jul 12, 2015 at 3:41

1 Answer 1

1

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());
}

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.