SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 2 down vote accepted

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

share|improve this answer

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.
share|improve this answer
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 '12 at 19:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.