I tried to add data to a list using a HTML Form and Javascript.

When I sent the data to the list I got TypeError: SP.ClientContext is undefined. After I cleared the cache and cookies everything worked fine (the data was sent and appeared in the list) but when I tried to send a new request the error appeared again, and didn't go away without clearing the cache.

How can I send my data without clearing the cache every time?

My code:

<table id="formular" style="width:800px; margin:0 auto">


     <td>Technician Name:</td><td><input type="text" name="tech_name"></td>


    <td>Technician's comments/recommendations:</td>
    <td><textarea name="tech_comments"></textarea></td>


    <td></td><td><button onclick="myFunction(event);">Send</button></td>





function myFunction(event){


var tech_name = $("input[name='WPQ2tech_name']").val();
var tech_comments = $("input[name='WPQ2tech_comments']").val();

    clientContext = new SP.ClientContext.get_current();

    var oList = clientContext.get_web().get_lists().getByTitle('Requests');


    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title', 'My New Item!');

    oListItem.set_item('Tech_name', tech_name);
    oListItem.set_item('Tech_comments', tech_comments);
    oListItem.set_item('Status', 'Pending');


        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)


function onQuerySucceeded() {
    console.log('Item created: ' + oListItem.get_id());
    alert("The Request has been sent");

function onQueryFailed(sender, args) {
    console.log('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
    alert("The request has not been sent. Please try again.");



I changed my code to



          //do all the stuff here - get the ids, update the list
    }, 'sp.js'); });

Now it seems it never reaches the function when I hit send. Any suggestions?


I got it working by using the code below but SP.ClientContext is undefined still appears.



        ExecuteOrDelayUntilScriptLoaded(myFunction(), "sp.js"); 


function myFunction(){
  • Try to surround with ExecuteOrDelayUntilScriptLoaded(function() { ... }, 'sp.js');
    – eirikb
    Mar 27, 2015 at 9:03
  • @eirikb Thank you for answering. Now it seems like it never reaces the function. I'll update in the question Mar 27, 2015 at 9:38
  • @eirikb I updated my question, please be so kind and help me to find the solution. Thank you Mar 27, 2015 at 9:41
  • Your updated code does not reflect my comment (missing 'sp.js)
    – eirikb
    Mar 27, 2015 at 9:53
  • @eirikb I forgot to add this part here, but it was in the code. Mar 27, 2015 at 9:57

4 Answers 4


A better way to use the Script On Demand is this

if(!SP.SOD.executeOrDelayUntilScriptLoaded(myFunction, 'sp.js')){ 

This worked for me

$(document).ready(function (){
 SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function()
    //function calls or code to be executed

I got it working by using SP.SOD.executeFunc("sp.js", "SP.ClientContext", myFunction);

The other function, ExecuteOrDelayUntilScriptLoaded doesn't work on published pages.

So here's the button:

<input type="button" onclick="addValue();" value="send">

And here's the function:

function addValue(){

    SP.SOD.executeFunc("sp.js", 'SP.ClientContext', myFunction);


function myFunction(){
//do all the stuff
  • You can accept your own answer if it's a valid solution. Please see this help page. Feb 16, 2016 at 13:39

Please add this to your asp page:

<SharePoint:ScriptLink Name="sp.js" runat="server" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink Name="sp.runtime.js" runat="server" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink Name="sp.core.js" runat="server" LoadAfterUI="true" Localizable="false" />

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.