Question

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());
            }
        
    });

           
});
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top