3

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">

<tr>

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

</tr>

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


<tr>

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

</tr>

</table>




</div>

<script>

function myFunction(event){


    event.preventDefault();
//alert("INTRA!");



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');

    //alert(oList);

    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');

    oListItem.update();

    clientContext.load(oListItem);
    clientContext.executeQueryAsync(
        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");
    window.location.reload();
}

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.");
}


</script>

EDIT

I changed my code to

$(document).ready(function(){


    ExecuteOrDelayUntilScriptLoaded(function(){

        $("#send_form").click(function(event){
          //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?

EDIT

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

$(document).ready(function(){

    $("#send_form").click(function(){

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

});

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

A better way to use the Script On Demand is this

if(!SP.SOD.executeOrDelayUntilScriptLoaded(myFunction, 'sp.js')){ 
  LoadSodByKey('sp.js');
};
3

This worked for me

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

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. – dstarkowski Feb 16 '16 at 13:39
0

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, privacy policy and cookie policy

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