I am currently creating a SharePoint 2013 hosted app on Office 365 and I currently have some JavaScript that inserts item into a host web list.
I wondering if it is possible to get the ID of the item that has just been created in the list using the current JavaScript code:
function Sumbit() { $('#submitBtn').click(function () { var ctx = new SP.ClientContext(appweburl); var appCtxSite = new SP.AppContextSite(ctx, hostweburl); var web = appCtxSite.get_web(); var List = web.get_lists().getByTitle('List Title'); var itemCreateInfo = new SP.ListItemCreationInformation(); var ListItem = List.addItem(itemCreateInfo); ListItem.set_item('Title', $('#title').val()); ListItem.set_item('Description', $('#Description').val()); ListItem.update(); ctx.load(ListItem); ctx.executeQueryAsync( Function.createDelegate(this, QuerySucceeded), Function.createDelegate(this, QueryFailed)); }); } function QuerySucceeded(sender, args) { console.log("success"); } function QueryFailed(sender, args) { console.log(args.get_message() + '\n' + args.get_stackTrace()); }
I have tried getting the ID inside of the 'QuerySucceeded' function by using:
ListItem.get_id();
However this was returning '-1' any help with this would be greatly appreciated.