Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
              }
SP.UI.ModalDialog.showModalDialog(options);
share|improve this question
add comment

2 Answers

up vote 1 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
add comment

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.
                item.Update();
             }
         }
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
add comment

Your Answer

 
discard

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.