0

Attempting to create a list item in an SP13 list using Javascript.

The JavaScript code is shown below.

When I click the button on the page the code runs up to the set_item statement and then just stops. However, if I paste the code into the debugger window I get my new record in my target list.

Any idea why the code just stops but runs in the debugger?

Using jquery 1.3.1 simply because another SP site was using that version and it ran successful.

Thanks.

$(document).ready(function(){

var siteURL="some url";



    $('#btn').click(function(){
        
        var clientContext = new SP.ClientContext("https://somedocs.net/sandbox");
        var oList = clientContext.get_web().get_lists().getByTitle('SubList1');
        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);

        oListItem.set_item('Title', 'My New Item!');
        
        oListItem.set_item('ProjectName', 'Hello World!');
        oListItem.set_item('Test1',"Hi Dax");
        oListItem.update();

        clientContext.load(oListItem);
        
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded), 
            Function.createDelegate(this, this.onQueryFailed)
        );
        function onQuerySucceeded() {
            alert('Item created: ' + oListItem.get_id());
        }

        function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + 


                   '\n' + args.get_stackTrace());
            }
        
    });

           
});

1 Answer 1

0

It may be that the required js file is not imported.

You could try to add this statement before your code.

ExecuteOrDelayUntilScriptLoaded(functionName, "sp.js");

Or

SP.SOD.executeOrDelayUntilScriptLoaded(functionName,"sp.js");

ExecuteOrDelayUntilScriptLoaded with sp.js when in debug mode Object Expected

1
  • Thanks so much!!! This worked...very much appreciate getting me past this sticking point.
    – Pablo
    Commented Sep 1, 2020 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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