3

I access my web in javascript code in SharePoint 2013. I'm using Jquery and KnockOutJS for my other functions. So I have added following urls above my code.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

At below line I get an exception says "Uncaught TypeError: undefined is not a function "

var context = new SP.ClientContext("http://myServer:1000/");

3 Answers 3

2

It seems that sp.js is not yet loaded when SP.ClientContext is being called.

Use SP.SOD.executeFunc to ensure that the specified file that contains the specified function is loaded and then runs the specified callback function.

You could try to enclose your code like this:

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {

     var webUrl = _spPageContextInfo.webAbsoluteUrl;
     var context = new SP.ClientContext(webUrl);
     //...

});
0
2

Use Below Pattern

$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(FunctionToRunWhenLoadIsFinished, "sp.js"); });



function FunctionToRunWhenLoadIsFinished() {

    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('ITSTotalDashboard_logs');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    this.oListItem = oList.addItem(itemCreateInfo);

    oListItem.set_item('Title', 'test');


    oListItem.update();

    clientContext.load(oListItem);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
1

This works for me when I am using WorkflowServices. It should work in your case as well:

ExecuteOrDelayUntilScriptLoaded(function () {
    ExecuteOrDelayUntilScriptLoaded(function () {
        SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
        SP.SOD.registerSod('SP.WorkflowServices.WorkflowServicesManager', SP.Utilities.Utility.getLayoutsPageUrl('SP.WorkflowServices.js'));
        SP.SOD.loadMultiple(['SP.ClientContext', 'SP.WorkflowServices.WorkflowServicesManager'], function () {
            var context = SP.ClientContext.get_current();
            var web = context.get_web();
            var servicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
            var instanceService = servicesManager.getWorkflowInstanceService();
        });
    }, "sp.js");
}, "sp.runtime.js");
1
  • 1
    Please don't add the same answer as this one Try to be more specific. Thank you
    – Benny Skogberg
    Commented Aug 26, 2016 at 14:51

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.