If I am using JavaScript Client Object Model and SP.UI.ModalDialog.showModalDialog to display a form to enter a new list item, how can I get the ID of that item after its been added?

Example javascript

var options = {};
options.url = "/YOUR_SITE/Lists/YOUR_LIST/NewForm.aspx";
options.dialogReturnValueCallback =
           function (dialogResult, returnValue)
                  console.log("dialogResult=" + dialogResult +
                              " returnValue=" + returnValue);
                  // dialogResult will be 1 if saved hit, 0 otherwise
                  // returnValue will be undefined

3 Answers 3


I think you will have to write a CAML query youself and retrieve it. Or use SPGetLastItemId method of SPServices library to get it.


Hi try below code hope it helps you and someone else if needed:

function GetInsertedId(PostedTitle,PostedPhone)
 var InsertedId= null;
     operation: "UpdateListItems",
     listName: "Results List",
     batchCmd: "New",
     async: false,
    valuepairs: [["Title", PostedTitle],["Phone", PostedPhone]
   completefunc: function(xData, Status) {
       var CreatedId= 
        InsertedId= CreatedId;
  return InsertedId; 

This might be worth a try.

Insert the list item using the SPListItem.Add() method?

This method, creates an item but requires the SPListItem.Update method to actually add the item to the list. In between, I suppose you would be able to get the Id of the list item.

Your code block would look something similar to this -

     using (SPSite site = new SPSite("http://localhost"))
            using (SPWeb web = site.OpenWeb())
                SPList list = web.GetList("/lists/announcements");
                SPListItemCollection items = list.Items;

                SPListItem item = items.Add();
                \\Retrieve the Id from the item object and do the manipulations required.
  • That won't work for 3 reasons. a) Thats standard object model code, not client object model/javascript. b) The ID isn't actually set until .Update is called. c) I need to use the standard NewForm.aspx to let the user enter the info and using this I would have to create my own form. Thanks though!
    – Ryan
    Feb 1, 2012 at 19:43

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.