3

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? – Robert Lindgren Aug 5 '16 at 8:05
  • 1
    Maybe paste ``SP.ClientContext is undefined```in that search box at the top right of the page? – Danny '365CSI' Engelman Aug 5 '16 at 8:13
7

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);
|improve this answer|||||
  • how to pass value in the Function? – Mavani Aug 5 '16 at 10:59
1

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 ...
}
|improve this answer|||||
0

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

|improve this answer|||||

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.