0

I am attempting to use the below code to add an item to my list. It is almost a direct copy from the MSDN website with the only changes being variable names.

If use this.oListItem = oList.addItem(itemCreateInfo); I get 'oListItem' is undefined error and nothing is added to the list.

If I change the above line to var oListItem = oList.addItem(itemCreateInfo); the it throws an error code SCRIPT5007: Unable to get property 'apply' of undefined or null reference but the list item is is added. However the onAddClassSucceeded is not trigger.

I know this has been asked in other post but I could not find acceptable answers for either situation. Changing this to var was one answer but it cause other issues.

Note: sp.js is loaded via the _spBodyOnLoadFunctionNames.push('_init');

How do rectify either of these issues?

    function createClassItem() {
        // Create a list item in the Classes list

    var ctx = new SP.ClientContext.get_current();
    var oList = ctx.get_web().get_lists().getByTitle('Classes');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreateInfo);
    //    debugger;
    oListItem.set_item('Title', '0');
    oListItem.set_item('reportDate', document.getElementById('reportDate').value);      
    oListItem.set_item('startDate', document.getElementById('startDate').value);
    oListItem.set_item('endDate', document.getElementById('endDate').value);


    oListItem.update();

    ctx.load(oListItem);

    ctx.executeQueryAsync(
        Function.createDelegate(this, this.onAddClassSucceeded),
        Function.createDelegate(this, this.onAddClassFailed));
}

function onAddClassSucceeded(sender, arg) {

    SP.UI.Notify.addNotification('Item created');
}

function onAddClassFailed(sender, args) {

    SP.UI.Notify.addNotification('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

1 Answer 1

0

Please try calling createClassItem after sp.js gets loaded like this

ExecuteOrDelayUntilScriptLoaded(createClassItem, 'sp.js');

Also add console logs in success and fail handlers to see what error its throwing if any.

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.