Using JavaScript Client Context Code:

 var clientContext = new SP.ClientContext.get_current();

after above Code execute error I get the following error

SP.ClientContext is undefined

in Client Context Code

  • Where have you put the code? Aug 5, 2016 at 8:05
  • 1
    Maybe paste ``SP.ClientContext is undefined```in that search box at the top right of the page? Aug 5, 2016 at 8:13

3 Answers 3


You got the error because the 'SP.ClientContext' in 'sp.js' is not loaded

Now, Add your context code in a function

function myFunction() {
 var clientContext = new SP.ClientContext.get_current();

Then, call the function as below which ensures the file 'sp.js' containing 'SP.ClientContext' function is loaded first and then runs 'myFunction'

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', myFunction);
  • how to pass value in the Function?
    – Mavani
    Aug 5, 2016 at 10:59
  • I hate SharePoint more everyday. Up to yesterday my code worked without loading SP.js, today not anymore...
    – Emaborsa
    Apr 6, 2023 at 8:38

You need to make sure sp.js file is loaded before you can access CSOM.

Use a code structured like this, in the same .js file:

$(document).ready(function () {
      //Do not execute jsom until sp.js file has loaded.
      ExecuteOrDelayUntilScriptLoaded(myFunctionToBeExecuted, "sp.js");

The ExecuteOrDelayUntilScriptLoaded(function1, jsFile.js) makes sure that your function1 is only called after jsFile.js is loaded. It's safer to place this within a jQuery $(document).ready(function(){}); to make sure your function gets called when everything else is ready.

Now you go ahead and just define your function. So at the end your code should look like:

$(document).ready(function () {
     //Do not execute jsom until sp.js file has loaded.
     ExecuteOrDelayUntilScriptLoaded(myFunctionToBeCalled, "sp.js");

function myFunctionToBeCalled(){
    var clientContext = new SP.ClientContext.get_current();
    // Rest of your code ...

SharePoint Client Side Object Model works a bit different as on the server side. Before you can use an object you must first load it and then you must execute a query to retrieve the object

Use ExecuteOrDelayUntilScriptLoaded Reference link

CSOM client context undefined

Client context undefined

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.