0

I have a SharePoint hosted app that creates a list instance. I'm trying the simple task of creating a list item but it doesn't work. No errors are displayed and I am not able to get into the success or failed methods when debugging.

function createListItem(appwebUrl) {
     var clientContext = new SP.ClientContext(appwebUrl);
     var oList =   clientContext.get_web().get_lists().getByTitle('CustomList');

     var itemCreateInfo = new SP.ListItemCreationInformation();
     this.oListItem = oList.addItem(itemCreateInfo);
     oListItem.set_item('Title', 'My New Item!');
     oListItem.set_item('CustomField Text', 'Hello World!');
     oListItem.update();

     clientContext.load(oListItem);
     clientContext.executeQueryAsync(
        Function.createDelegate(this, onQuerySucceeded),
        Function.createDelegate(this, onQueryFailed)
   );
}


 function onQuerySucceeded() {
    alert('Item created: ' + oListItem.get_id());
 }

 function onQueryFailed(sender, args) {
     alert('Request failed. ' + args.get_message() +
    '\n' + args.get_stackTrace());
}

I've also tried this with REST and the same thing occurs. No error, it just doesn't work. What's up with my App?

0

First make sure the function is getting invoked properly. Then change below

this.oListItem = oList.addItem(itemCreateInfo);

to

oListItem = oList.addItem(itemCreateInfo);
  • Remove "this." didn't change anything. I'm invoking the method on a button click like so createListItem(_spPageContextInfo.siteAbsoluteUrl); – Michael Colbs Aug 3 '16 at 21:41
  • Have you tried adding some alert statement just to make sure how far it goes. – Amal Hashim Aug 3 '16 at 21:46
  • I can debug right up to "clientContext.executeQueryAsync". The alerts shown in the code never fire. – Michael Colbs Aug 3 '16 at 21:50
  • Remove this from the functions.. Also check if the item actually got created or not. – Amal Hashim Aug 3 '16 at 22:29
  • I removed 'this' as you can see in my above edit. The item is definitely not created – Michael Colbs Aug 5 '16 at 18:33

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.