Monday 25 August 2014

Start SharePoint Workflows with JavaScript Client Object Model

This will be a quick post to follow up on couple of my previous posts. In this post we will see how to start SharePoint 2013 Workflows which are created using the SharePoint 2010 Workflow engine. If you have Workflows created with the SharePoint 2013 engine, please see my previous posts:

1) Managing SharePoint 2013 Workflows with CSOM

2) Using the SharePoint 2013 Workflow Interop Service in CSOM

In this post, I will show you basically the same thing as in the previous posts, but by using the JavaScript Client Object Model (JSOM).

I recently had a requirement for which I had to start a Nintex Site Workflow with JavaScript. Now due to my previous posts, I had an idea that we can use the Managed CSOM to manage workflows but wanted to see if the same is possible from the JavaScript CSOM.  Nintex Workflows utilize the SharePoint 2010 Workflow Engine and hence we require the Interop Service to work with them in JavaScript.

Here is the code I put together to start my Site Workflow:



You can  have a look at my previous posts and the SP.WorkflowServices.debug.js file to see how to start other workflows such as List Workflows or SharePoint 2013 Workflow Engine workflows from the JavaScript Client Object Model.


22 comments:

Ramesh Chilakala said...

hello Vardhaman,

nice post on how to start a workflow with Javascript client.
I have tried to deploy your code by placing in Web Content Editor in Site home page.

is it right way of Deploying? please suggest the deployment steps. ThankYou :)

Vardhaman Deshpande said...

Hi Ramesh,

Since its all just JavaScript, you can deploy this and use it just like you deploy the JavaScript for your solutions. Content Editor or Script Editor WebPart, or a JavaScript file embeded in a page are just some examples which come to mind.

Jai Prasath said...

Hi Vardhaman,
I have tired to start a site level workflow by using javascript. All the steps are followed as mentioned in your post eventhough workflow is not starting for me. Any idea please it is much helpful.

Vardhaman Deshpande said...

Hi Jai Prasath,

Is it a Site Workflow created with the SharePoint 2013 Workflow Platform? If yes, then this code will not work as the interop service is only meant to start the workflows created with the SharePoint 2010 platform.

However, what you can do is have a look at my previous post about starting SP 2013 site workflow with CSOM and try to replicate the same code in the JSOM.

Jai Prasath said...

Hi Vardhaman,
Thanks for your response. I hope Nintex 2013 workflow still using all the methods and functions from SharePoint 2010. Any idea why interop service is not helping to start Nintex workflow.

Vardhaman Deshpande said...

Do you get any errors? Does the success callback get executed? Also make sure that Workflow Manager is installed on the farm. (I assume you are On-Premises)

Unknown said...

hello Vardhaman,

I am trying to start a site workflow in SharePoint 2013 with javascript. I did exactly as you described, but I receive the following error: GetWorkflowInteropService is null
I do not have the SP 2013 workflow manager setup running, so my workflow is a SP2010 workflow created in SP 2013.

I hope you can help me out

Vardhaman Deshpande said...

Hi Rene,

Unfortunately the SP.WorkflowServices.WorkflowServicesManager class is part of the Workflow Manager. So you will need that to be installed on the farm even if you are using the Interop service and calling in SharePoint 2010 Workflows

Unknown said...

Hello,
It seems my previous comment was not sent.

We have Sharepoint 2013 Online. The code works great for 2010 site workflows but does not work for 2013.
It writes this error: "associationName" Any suggestions?

Vardhaman Deshpande said...

Hi Jan,

The code in this post uses the Interop service which only works with Workflows created using the SP2010 Workflow engine.

To start SharePoint 2013 engine workflows, take a look at this post:
http://www.vrdmn.com/2014/05/managing-sharepoint-2013-workflows-with.html

It uses the .net CSOM for starting SP2013 workflows. You will have to replicate the same functionality in the JavaScript Client Object Model.

Hope this helps.

Unknown said...

Ah, ok thank you. I was confused by the title

Sri Vikas said...

Hi Vardhaman,

I am using SharePoint 2010 workflows on a SharePoint 2013 Foundation server.

I get the following error when executing your code. "WorkflowServicesManager is not defined". I googled for it and found that it is because Workflow Manager is not installed. So I installed and configured it.

But when I try to register using Register-SPWorkflowService, it throws an error saying the cmdlet is not recognized. I came to know that it is only for SharePoint server editions. I also learnt that there is no help from REST API as well. So I am struck here. Can you please help me how to use this for SharePoint foundation. Thanks much in advance.

Regards,
Srivikas.

Vardhaman Deshpande said...

Hi Sri Vikas,

I understand your problem. But the thing is the Workflow Manager cannot be used in SharePoint Foundation 2013. Only in the Standard and Enterprise version.

The reason for this being that there is no User Profile Service Application in SharePoint Foundation 2013. Workflow Manager uses REST + OAuth for communication between SharePoint and the Workflow Engine (hosted on the Workflow Server) and the User Profile Service Application is required for OAuth.

Sri Vikas said...

Hi Vardhaman,
Thank you very much for your fast response.

Is there any other way to trigger a SP 2010 workflow from JSOM? I've gone throgh the SP.Workflow namespace in sp.js, but couldn't get any help.

Regards,
Srivikas.

Vardhaman Deshpande said...

I am afraid not. Both the CSOM and JSOM require the Workflow Manager to interact with either 2010 or 2013 engine Workflows.

Sri Vikas said...

Thank you Vardhaman. Will search for other alternatives.

Gavin said...

Hi,

I'm trying to Start a 2010 workflow via provider hosted app (a site request is added to the List by the app, then for some reason it won't start automatically - I assume because the App Account is viewed as a System Account, so trying to start it automatically). I get permission denied. Any ideas?

Ruwantha Ratnayake said...

Hi, on the above approach the workflow will be triggered by logged-in\ current user. Can we trigger the workflow from a different user (dedicated account for workflows) ?

Unknown said...

Dude, by any chance if you can help me to trigger Nintex workflow that is deployed in SharePoint Online (Office 365), that would be great. The Nintex workflow nowadays deployed as SharePoint 2013 Workflow, but I can't get it run with Javascript.

I've tried the code, but nothing worked.

Vardhaman Deshpande said...

@Indriani Please have a look at my previous post: http://www.vrdmn.com/2014/05/managing-sharepoint-2013-workflows-with.html

It shows you how to start a 2013 workflow with CSOM. You will have to write the equivalent JSOM code.

Error said...

Hi ,

when I was debug my code I have issue this erroer:

Uncaught TypeError: this.get_context is not a function
at SP.WorkflowServices.WorkflowServicesManager.getWorkflowInteropService (sp.workflowservices.js:1)
at StartSiteWorkflow (AdminGridScript_A.js:35)
at EnsureScript (init.js:1)
at Object.EnsureScriptFunc [as executeFunc] (init.js:1)
at SodCallbackInfo.success (AdminGridScript_A.js:9)
at c (init.js:1)
at b (init.js:1)
at NotifyOnLoad (init.js:1)
at init.js:1
at HTMLScriptElement. (init.js:1)
getWorkflowInteropService @ sp.workflowservices.js:1
StartSiteWorkflow @ AdminGridScript_A.js:35
EnsureScript @ init.js:1
EnsureScriptFunc @ init.js:1
(anonymous) @ AdminGridScript_A.js:9
c @ init.js:1
b @ init.js:1
NotifyOnLoad @ init.js:1
(anonymous) @ init.js:1
(anonymous) @ init.js:1
load (async)
Define_module_def.Define.loadScript @ init.js:1
LoadSodInternal @ init.js:1
LoadSod @ init.js:1
b @ init.js:1
NotifyOnLoad @ init.js:1
(anonymous) @ init.js:1
(anonymous) @ init.js:1
load (async)
Define_module_def.Define.loadScript @ init.js:1
LoadSodInternal @ init.js:1
LoadSod @ init.js:1
b @ init.js:1
NotifyOnLoad @ init.js:1
(anonymous) @ init.js:1
(anonymous) @ init.js:1
load (async)
Define_module_def.Define.loadScript @ init.js:1
LoadSodInternal @ init.js:1
LoadSodInternal @ init.js:1
LoadSod @ init.js:1
LoadSodByKey @ init.js:1
EnsureScript @ init.js:1
EnsureScriptFunc @ init.js:1
EnsureScriptParams @ init.js:1
C @ sposuitenav.js:1
w @ sposuitenav.js:1
GetSuiteLinks @ init.js:1
r @ sposuitenav.js:1
RenderSuiteLinksFromShellData @ sposuitenav.js:1
(anonymous) @ All Device.aspx:1034
init.js:1 [Violation] 'load' handler took 3752ms

Unknown said...

Hi Vardhaman, I am trying to cancel / terminate a SP 2010 workflow in SP 2013 Foundation site. I understand the InteropService.CancelWorkflow should do the trick but I am unable to get the instance id of the workflow that is in progress on list item. Can you please help?