1

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
    See 1 & 2. – moe Jun 27 '17 at 5:37
2

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

|improve this answer|||||
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
|improve this answer|||||
  • 1
    We can consider using CreatedBy id, so in case of multi user environment you will get the last inserted by same user – codemirror Jul 4 '17 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));
        }
    });
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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