0

My code executes if the console window is opened for atleast one time in IE 11 otherwise not running.

I found various articles and posts stating removing "console" object and/or disabling cache during ajax calls will prevent this issue. But in my code there is no ajax calls nor console objects. Still the problem persists.

Below is the as usual code for inserting item which works if console is opened.

            ...
            ...
            var list= webSite.get_lists().getByTitle('listname')
            var listItemInfo = new SP.ListItemCreationInformation()
            var listItem = list.addItem(listItemInfo)
            ...
            ...
            listItem.update()
            clientContext.load(listItem);
            clientContext.executeQueryAsync(function () {},function () {});

Couldn't understand the actual reason for this behaviour. Have anyone faced this behaviour before.

Is there any way to stop this issue.

Side Note :
If I modify the source code like adding alert(), my code runs for the first time, but next time onwards the same issue. So I guess there must be some type of caching issue.

2
  • did you check if any error are coming in function complete ? try to capture xdata and status in complete function.... Commented Dec 23, 2016 at 16:28
  • @SiddharthVaghasia. No errors are coming.
    – Unknown
    Commented Dec 26, 2016 at 5:00

1 Answer 1

0

What you need to do is to make sure that sp.js file is loaded and after that add the list item

Try code as below:

function createListItem() {

    var siteUrl = '/sites/MySiteCollection';
    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle("listname");

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);

    oListItem.set_item('Title', 'My New Item!');        

    oListItem.update();

    clientContext.load(oListItem);

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

function onQuerySucceeded() {

    //console.log('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {

    //console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem);
2
  • Those Function.createDelegate statements are old specific IE8 code, it will work for now because SharePoint polyfills them, your code might not work in the future Commented Dec 25, 2016 at 12:11
  • Thankyou for the answers. Including sp.js made my code run but new issue occured. Actually I was trying to override the default behaviour of the publish button in NewPost.aspx page. All I wanted to do is save the post in list myself for some reason. On overriding it worked perfectly in all browsers except IE. In IE two posts are created since, both my function and default behaviour is executed. So I made my logic available inside the PreSaveAction method and returned false. Now its working correctly.
    – Unknown
    Commented Dec 26, 2016 at 5:32

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.