cancel
Showing results for 
Search instead for 
Did you mean: 
Workflow Hero

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
Workflow Hero

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>

Accept as Solution Reply
Workflow Hero

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
Accept as Solution Reply
Workflow Hero

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

Accept as Solution Reply
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
Accept as Solution Reply
Workflow Hero

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.

Accept as Solution Reply
Workflow Hero

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.

Accept as Solution Reply
Workflow Hero

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.

Accept as Solution Reply
Not applicable

Re: Start workflow with a button

Anyway you can share more details about your solution?  I am trying to create a button that will perform a javascript function to launch a workflow that duplicates the current form (into a new record) and populates/carries over some data. 

Appreciate any assistance.

0 Kudos
Accept as Solution Reply
Workflow Hero

Re: Start workflow with a button

Kevin,

Sure, you will need SharePoint Designer to add the custom button. There you can create Custom Actions for any list/library. In the custom action you want the "Navigate to URL" option. Here you will put the URL to start either a list workflow or a site workflow. We've used both. The site workflow, when users don't have edit permission to a list/lib, which then calls the list/lib workflow for them.

The URL is structured as follows:

http://SP-site.com/_layouts/15/NintexWorkflow/StartWorkflow.aspx?WorkflowName=MyWorkflow

You will need an image to place in the ribbon that gets added to the custom action in two sizes; 32x32 and 16x16. These can be saved to one of the site asset libraries if you like.

Regards

Accept as Solution Reply