3

I am trying to access information about running workflows in a SharePoint list but I am running into problems with the workflow services JSOM library. I am using the workflow services just like every example I can find, see code below:

var context = SP.ClientContext.get_current();
var web = context.get_web();
var servicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
var instanceService = servicesManager.getWorkflowInstanceService();

While executing the code, the last line in the above snippet throws an exception

TypeError: this.get_context is not a function.

Chris Odegard
  • 105
  • 1
  • 6
Travis L
  • 43
  • 5

2 Answers2

2

Propbaly this error occurs since one of the specified files from SharePoint JavaScript library has not been loaded.

  • SP.js
  • SP.Runtime.js
  • SP.WorkflowServices.js

To ensure that the specified file(s) has been loaded you could consider the following approach:

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 ctx = SP.ClientContext.get_current();
    var workflowServicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web()); 
    var workflowSubscriptionService = workflowServicesManager.getWorkflowSubscriptionService();                
    //...    

});

SP.SOD.loadMultiple function is intended for loading on demand scripts which in turn is a part of SharePoint JavaScript Library. Alternatively you could utilize jQuery.getScript() from jQuery library.

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • I thought about that as well and do not believe that to be the case. I have taken a different approach by using the ExecuteOrDelayUntilScriptLoaded methods so that the above code has been wrapped with: ExecuteOrDelayUntilScriptLoaded(function () { ExecuteOrDelayUntilScriptLoaded(function () { ExecuteOrDelayUntilScriptLoaded(function () { }, "sp.workflowservices.js"); }, "sp.runtime.js"); }, "sp.js"); I'll will modify the code to try the SOD approach and see if makes a difference though. – Travis L Jun 19 '15 at 20:07
1

@VadimGremyachev, I tried using your code example and I then got a TypeError related to SP.Utilities. I guess SP.Utilities is part of sp.js so it couldn't be used to load itself. The only way in which I was able to get it working correctly was to use both the SOD and ExecuteOrDelayUntilScriptLoaded methods. I don't know if this is a nuance of using this using client-side rendering to override a list view or just bad timing of script loading. The code that works is as follows:

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");`
Travis L
  • 43
  • 5
  • This worked for me. I did not try @vadim-gremyachev answer because I've been fighting with this 2013 script on demand crap for three days. The other answer might work but I am so, so, so, so, so done. Thank you travis-l – Kristopher Aug 26 '16 at 13:03