2

I am creating a new item using NewForm.aspx opened using SP showModalDialog. How can I get the ID of created item after form is saved?

I need to get the ID to do some processing.

1
  • 1
    See 1 & 2. Commented Jun 27, 2017 at 5:37

3 Answers 3

3

I solved this using SPServices

var lastID = $().SPServices.SPGetLastItemId({ listName:'List 1' }); 

this will get last inserted item's id which was created by same user.

List 1 is list's name

1

You can try running a SP2013 REST api and

get top 1 item while they are sorted by Created date in descending order

once your model dialog saves data in the list. Please refer this:

 _api/web/Lists/GetByTitle('your_List_title')/items?$top=1&$select=ID&$orderby=Created desc 
1
  • 1
    We can consider using CreatedBy id, so in case of multi user environment you will get the last inserted by same user Commented Jul 4, 2017 at 11:28
0

I like to use the out of the box modal as a template and utilize the "dialogReturnValueCallback".

// initialize your app name and guid var appListName= 'YOUR LIST NAME'; var appListGuid = 'YOUR LIST GUID'; // onclick attribute to place if you have a custom button onclick="displayCustomModalApp(\''+_spPageContextInfo.webServerRelativeUrl+'/Lists/'+appListName+'/NewForm.aspx?&IsDlg=1\')" // custom modal using modified SharePoint OOB modal method function displayCustomModalApp(mUrl, width, height, title) { var options = { url: mUrl, width: width, height: height, title: title, dialogReturnValueCallback: function(dialogResult){ // only reset page if item is saved, do nothing if cancelled or closed if(dialogResult){ // run the function to get last item Id when the dialog closes retrieveLastItemID(); } } }; SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options); } // function to retrieve the last created item Id from current logged in user function retrieveLastItemID() { // filter by current author id and order by 'Id desc' to get the latest var siteUrl = _spPageContextInfo.webAbsoluteUrl; var fullUrl = siteUrl + "/_api/Web/Lists(guid'"+appListGuid+"')/items?$select=Id&$filter=Author/Id eq '"+_spPageContextInfo.userId+"'&$orderby=Id desc&$top=1"; $.ajax({ url: fullUrl, type: "GET", headers: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", }, success: function(data) { var lastId = data.d.results[0].Id; // DO STUFF! }, error: function(error){ console.log(JSON.stringify(error)); } }); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.