cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Automation Master
Automation Master

Start workflow with a button

Hi,

In O365, to start the workflow, I would like to click on a button I have inserted with some HTML in a computed column.

If I use the onclick event using the same Javascript used when you click on the Workflow name in the Workflow page, it fails as it cannot find the Javascript function.

For example: The Javascript in the Workflow Page is

javascript:StartWorkflow4('5364449-4753-45a6-9d03-2cd1a3128152', '1', '{69F81205-A555-4568-A673-1528C546E3B7}')

FYI, the 3 arguments are (subscriptionId, itemId, itemGuid)

The Javascript fails because StartWorkflow4 is not found. I have tried to reference the page where the Javascript is coded but the debugger told me that the page name is unkwown !!!

Then I tried to use a full url to set in a A HREF, but I cannot find any reference on how to build it.

Did you have any success to build the same feature.

Thanks!!!

Christophe

Labels: (1)
Reply
24 Replies
Highlighted
Automation Master
Automation Master

Re: Start workflow with a button

This is that function:

<script type="text/javascript">

  var errorMessage = "Something went wrong. To try again, reload the page and then start the workflow.";

  var theForm = document.forms['aspnetForm'];

  if (!theForm)

  {

  theForm = document.aspnetForm;

  }

  function StartWorkflow(iwa)

  {

  var elIwaStart = document.getElementById("iwaStart");

  elIwaStart.value = iwa;

  theForm.submit();

  }

  var dlg = null;

  function StartWorkflow4(subscriptionId, itemId, itemGuid)

  {

  showInProgressDialog();

  var ctx = SP.ClientContext.get_current();

  var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());

  var subscription = wfManager.getWorkflowSubscriptionService().getSubscription(subscriptionId);

  ctx.load(subscription, 'PropertyDefinitions');

  ctx.executeQueryAsync(

  function(sender, args)

  {

  var params = new Object();

  var formData = subscription.get_propertyDefinitions()["FormData"];

  if(formData != null && formData != 'undefined' && formData != "")

  {

  var assocParams = formData.split(";#");

  for(var i = 0; i < assocParams.length; i++)

  {

  params[assocParams] = subscription.get_propertyDefinitions()[assocParams];

  }

  }

  if(itemId)

  {

  wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, params);

  }

  else

  {

  wfManager.getWorkflowInstanceService().startWorkflow(subscription, params);

  }

  ctx.executeQueryAsync(

  function(sender, args)

  {

  closeInProgressDialog();

  var elWf4Start = document.getElementById("wf4Start");

  elWf4Start.value = 1;

  theForm.submit();

  },

  function (sender, args)

  {

  closeInProgressDialog();

  alert(errorMessage);

  }

  );

  },

  function(sender, args)

  {

  closeInProgressDialog();

  alert(errorMessage);

  }

  );

  }

  function closeInProgressDialog()

  {

  if(dlg != null)

  {

  dlg.close();

  }

  }

  function showInProgressDialog()

  {

  if(dlg == null)

  {

  dlg = SP.UI.ModalDialog.showWaitScreenWithNoClose("Please wait...", "Waiting for workflow...", null, null);

  }

  }

  function HandleCheckinBeforeStartWorkflow()

  {

  var strError = "Please check this document in before starting a workflow.";

  window.alert(strError);

  }

</script>

Reply
Highlighted
Automation Master
Automation Master

Re: Start workflow with a button

Though I posted the function, I tried using it as a javascript button on a form and I had a success message, but no workflow was started though I gave it the right parameters.

So I'm not sure yet how to use it on a Nintex Form. But that would be nice if we can

0 Kudos
Reply
Highlighted
Automation Master
Automation Master

Re: Start workflow with a button

Hi Andrew,

Thanks for searching. I have the same result as you after putting this code in a Snippet in the view.

The line on which the javascript stops is

var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());

Maybe because there is someting from the Nintex Workflow page that is missing in the form or in the view.

Best regards,

Christophe

Reply
Highlighted
Not applicable

Re: Start workflow with a button

All, great conversation here.

Andrew, great seeing your name pop so much on the Connect site. Hope all is well.

As I read through the thread, and I eyeball the code, it looks to me like this is code to start a workflow where we would NOT need to collect initiation parameters from the end user. Am I reading it correctly?

The reason I ask, and the reason I stumbled onto this thread, is that I am trying to put a button on a form that takes the user to the Start Workflow page, which would then prompt for the initiation variables.

What I've been able to figure out is that the link to the page looks like Site URL/_layouts/15/NintexWorkflow/StartWorkflow.aspx?List={List ID}&ID=ID&TemplateID={59e379ea-f667-4327-8090-d6aeae10b2d0}&Source=Item URL.

I can get everything but the TemplateID from the item properties, and I noticed that templateID changes every time you republish the workflow.

I wonder if the trouble you're having has to do with the subscription ID, and the fact that it changes when you republish the workflow. Any thoughts on how subscriptionID could be figured out dynamically?

0 Kudos
Reply
Highlighted
Nintex Newbie

Re: Start workflow with a button

I ran into the workflow template ID being dynamic as well. I was trying to use calculated fields to present a series of links to launch workflows. I ended up using a Nintex workflow to build the the hyperlinks and then adding them to a rich text field. Note that you can also modify the source parameter in the link so that when the workflow is done initializing, it will take you to an alternate location. I used this method to allow a user to open up a list item that uses a Nintex form. On the Nintex form, it has a list view of related items and the workflows links for those items. So when the user clicks a link, it runs the workflow and then returns them to the list item instead of dumping them back to the list parent list view.

I did some initial searching and didn't find an easy solution to the template ID issue. We've just settled on manually updating it if the workflow changes. The other solutions I saw seemed to involved programming.

Reply
Highlighted
Nintex Newbie

Re: Start workflow with a button

Ben, you can change the "TemplateID=..." to WorkflowName=MyWorkflow

That way when you republish the workflow it will still point to it.

Reply
Highlighted
Nintex Newbie

Re: Start workflow with a button

OK - that's just silly. In all of my searching, no mention of WorkflowName as a valid parameter! Thanks Gerard, my testing shows this works. Very helpful.

Reply
Highlighted
Nintex Newbie

Re: Start workflow with a button

I am getting the below error when I try to do the same

Error: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). undefined

0 Kudos
Reply
Highlighted
Nintex Newbie

Re: Start workflow with a button

Kalyan, here's some detail on how to use this option.

The web URL is the first part: http://my.local.dom

This is followed by the reference to the start workflow aspx page (for Nintex in this case): /_layouts/15/NintexWorkflow/StartWorkflow.aspx

To reference the workflow name simply add: ?WorkflowName=MyWorkflow

So the complete URL looks like: http://my.local.dom/_layouts/15/NintexWorkflow/StartWorkflow.aspx?WorkflowName=MyWorkflow

If you want to add a source path that the page returns to when they start the workflow or click Cancel, you can add the following to the end:
&Source=http://my.local.dom/myhomepage.aspx

Hope this helps,

Gerard

0 Kudos
Reply