1

I have a script that begins as follows:

 SP.SOD.executeFunc('sp.js','SP.ClientContext',auditCheck);

 function auditCheck() {
 var clientContext = new SP.ClientContext();
 var oList = clientContext.get_web().get_lists().getByTitle('AuditList');
 var itemCreateInfo = new SP.ListItemCreationInformation();
 var oListItem = oList.addItem(itemCreateInfo);
 oListItem.set_item('Title','audit');
 oListItem.update();
 clientContext.executeQueryAsync(succeed1, failure1);
 }

 $(document).ready(function() {

 $('.button1').click(function() {
 var clientContext = new SP.ClientContext();
 var oList = clientContext.get_web().get_lists().getByTitle('List1');
 var itemCreateInfo = new SP.ListItemCreationInformation();
 var oListItem = oList.addItem(itemCreateInfo);
 oListItem.set_item('Title','ThisTitle');
 oListItem.update();
 clientContext.executeQueryAsync(succeed2, failure2);
 });
 }); //end ready

Do I need to load SP.ClientContext again, using SP.SOD.executeFunc, when firing off the button click $('.button1') function?

Previously, I was getting an error

Object does not support this action SP.ClientContext.

But after adding SP.SOD.executeFunc, it seems to be okay. The strange thing is I tested in two user groups: Full Permissions and Read Only.

The Read Only group got the error but when I moved them to the Full Perm group, they no longer got the error. This was PRIOR to adding in the SP.SOD.executeFunc. AFTER adding the SP.SOD.excuteFunc, I added the users back to the READ ONLY group and the error no longer occurs. I'm guessing the group exchange didn't have anything to do with it but it was more of a timing issue.

However, I do want to make sure that SP.ClientContext is loaded for all functions calling on it.

0

We get error Object does not support this action SP.ClientContext OR SP.ClientContext is undefined when the code that uses the ClientContext object runs even before loading SP.JS.

This happens because The SP.JS file has the ClientContext object used for representing the context of SharePoint Objects and operations.

SO you just need to make sure that SP.JS gets loaded properly before custom JavaScript will be executing that uses ClientContext object. There is no need load SP.ClientContext again, using SP.SOD.executeFunc.

Refer below code for better understanding on how to use SP.SOD.executeFunc :

$(document).ready(function() {
    SP.SOD.executeFunc('sp.js','SP.ClientContext',readyFunction);
});

function readyFunction(){

    auditCheck();
    $('.button1').click(btnClickHandler);

}

function auditCheck() {
    var clientContext = new SP.ClientContext();
    var oList = clientContext.get_web().get_lists().getByTitle('AuditList');
    var itemCreateInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title','audit');
    oListItem.update();
    clientContext.executeQueryAsync(succeed1, failure1);
}

function succeed1(){
    // auditCheck success handler
}

function failure1(){
    // auditCheck failure handler
}


//button click handler
function btnClickHandler(){
    var clientContext = new SP.ClientContext();
    var oList = clientContext.get_web().get_lists().getByTitle('List1');
    var itemCreateInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title','ThisTitle');
    oListItem.update();
    clientContext.executeQueryAsync(succeed2, failure2);
}


function succeed2(){
    // btnClickHandler success handler
}

function failure2(){
    // btnClickHandler failure handler
}
  • Rohit: So the way I have it written now, will that work okay? I want the audit function to fire off when the SP.ClientContext loads. The audit function is the first function listed. Is that enough to cover any other functions in doc.ready that are referencing the ClientContext when a user clicks a button? – JackSki Aug 30 '17 at 17:14
  • @JackSki You code should also work fine. Because your auditCheck function will get called after SP.JS gets loaded properly. And for the button click handler, by the time button click event handler gets called (user clicks on button), SP.JS generally get loaded fully as it gets enough time. But it is always better to make sure the SharePoint script file SP.JS is loaded before your code runs(that uses ClientContext object) . – Rohit Waghela Aug 30 '17 at 17:59
0

You can wrap all your functions inside a mainFunction and call it once in your SP.SOD.executeFunc :

SP.SOD.executeFunc('sp.js','SP.ClientContext',mainFunction );

function mainFunction(){
  $(document).ready(function() {
    $('.button1').click(createList);
  });
  auditCheck();
}

function auditCheck() {
 var clientContext = new SP.ClientContext();
 var oList = clientContext.get_web().get_lists().getByTitle('AuditList');
 var itemCreateInfo = new SP.ListItemCreationInformation();
 var oListItem = oList.addItem(itemCreateInfo);
 oListItem.set_item('Title','audit');
 oListItem.update();
 clientContext.executeQueryAsync(succeed1, failure1);
 }

function createList(){
var clientContext = new SP.ClientContext();
 var oList = clientContext.get_web().get_lists().getByTitle('List1');
 var itemCreateInfo = new SP.ListItemCreationInformation();
 var oListItem = oList.addItem(itemCreateInfo);
 oListItem.set_item('Title','ThisTitle');
 oListItem.update();
 clientContext.executeQueryAsync(succeed2, failure2);
}

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.