3

I want to use something like the following in a SharePoint Online add-in to delay the firing of resetPage():

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

but get the error "Uncaught ReferenceError: SP is not defined(…)"

What reference do I need or should I use another approach?

2
  • sharepoint.stackexchange.com/questions/102003/… - refer this, it might help.. Commented Dec 11, 2016 at 16:43
  • Why are you trying to get the clientcontext only to delay the execution of resetPage? That can be done by adding a setTimeout(resetPage, 1337) also and without loading a library for no reason.
    – Mx.
    Commented Dec 12, 2016 at 16:45

2 Answers 2

1

Uncaught ReferenceError: SP is not defined means the SP.JS is not loaded properly, to load it Try to format the code as the following :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script>

   $(document).ready(function(){
        var scriptbase = "https://<sharepoint online site url>" + "/_layouts/15/";
    //Do not change the order, scripts should load by order.  
     $.getScript(scriptbase + "init.js",
            function () {
                $.getScript(scriptbase + "SP.Runtime.js",
                   function () {
                $.getScript(scriptbase + "SP.js", execOperation);
              });
          });
           });



function execOperation() {
  // **add your code**
 context.executeQueryAsync(onSuccess, onFail);
}

function onSuccess() {

}

function onFail(sender, args) {
    alert('Error:' + args.get_message());
}
</script>
0

I used include all the requires jS as per the below sequence and there by I was able to include the SP.js

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="/_layouts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="/_layouts/sp.core.js" type="text/javascript"></script>
    <script src="/_layouts/sp.runtime.js" type="text/javascript"></script>
    <script src="/_layouts/sp.js" type="text/javascript"></script>

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.