0

I am writing some javascript just to retrieve some simple properties.

I have added a script editor webpart to the page and added simple javascript to makesure I can access the client context of the current web however I am getting and error.

This is in office 365.

$(document).ready(function () {

$("p").click(function(){
    $(this).hide();
  });
  alert("hello");
  alert("new Change");
  getUserName();


  function getUserName() {
      var context = SP.ClientContext.get_current();
      var site = context.get_web();
  }
});

Need to resolve it as I am doing something very silly.

0

Make sure you wait for the script to load using the below block

SP.SOD.executeOrDelayUntilScriptLoaded(function() { 
    console.log(SP.ClientContext);
    //code here
}, "sp.js");

Otherwise you can't access it

  • Does that go within Document Ready Onzur? – Miyagi Jul 7 '14 at 17:37
  • @Miyagi not for the client context but you need it for changing the DOM using jQuery. Since you are using the client contet in the getUsername function only, you can put the SP.SOD block inside your getUserName function – onzur Jul 7 '14 at 17:48
0

change to the following :

function getUserName() {
   SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){ 
     var context = SP.ClientContext.get_current();
      var site = context.get_web(); 
   });    
}

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.