var context = SP.ClientContext.get_current(); 

throws this error,

Uncaught ReferenceError: SP is not defined

the snippet works fine in SharePoint 2013, Are there .js that I need to load? if yes, how can I make this to work!

Thanks!

Try to load the sp.js as the following

var clientContext;

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

function Getdata() {
        try {
            clientContext = new SP.ClientContext.get_current();                   
        }
        catch (err) {
            alert(err);
        }
    }

If the above code do not working try to load init.js, SP.Runtime.js and SP.js as the following

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


   $(document).ready(function(){
        var scriptbase = "http://SiteURL" + "/_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", Getdata);
              });
          });
           });


function Getdata()
{
   var context = new SP.ClientContext.get_current();
    // your code

context.executeQueryAsync(
      function(){ 

      //On success function

     },
     function(){ 
      //On fail function

     }
  );
}
</script>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.