cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

how to start a workflow on button click using javascript in sharepoint

Jump to solution

I want to start a workflow using a nintex form button onclick event .

Labels: (3)
0 Kudos
Reply
10 Replies
Workflow Hero

Re: how to start a workflow on button click using javascript in sharepoint

Jump to solution

First you have to get the ID of your workflow.

Then use something like this (this is my code for O365 Workflow starting...didn't try OnPremise):

function startWorkflow()

{

  try

  {

  showInProgressDialog();

  var ctx = SP.ClientContext.get_current();

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

  var subscription = wfManager.getWorkflowSubscriptionService().getSubscription("ID OF YOUR WORKFLOW");

  ctx.load(subscription, 'PropertyDefinitions');

  ctx.executeQueryAsync(

  function(sender, args)

  {

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

  ctx.executeQueryAsync(

  function(sender, args)

  {

  closeInProgressDialog();

  },

  function (sender, args)

  {

  closeInProgressDialog();

  alert(errorMessage);

  });

  });

  }

  catch(ex)

  {

  alert(ex);

                dlg.close();

  }

}

Reply
Workflow Hero

Re: how to start a workflow on button click using javascript in sharepoint

Jump to solution

Hi,

Find below an example to start a site workflow (with association data) from a custom sharepoint modal dialog. For a list workflow you just have to adapt the header SOAPAction and the body regarding the definition of your SOAP Action.

My click button : Function.createDelegate(this, _startClickHandler)

***************************************

function _startClickHandler(e){

     e = e||window.event;

      e.preventDefault();

     // Get a custom field filled : example wtih a comment

     var comments=$get("ctl00_PlaceHolderMain_Comment",_form).value;

     if(comments == null || comments == ""){

          Sys.Debug.trace("Comment is mandatory");

          alert("Comment is mandatory");

          return;

     }

     // Call my custom function for starting workflow

     _startWorkflow(function(){

          window.commonModalDialogClose(SP.UI.DialogResult.OK, null);

          _dialogReturnValueCallback(SP.UI.DialogResult.OK);

     }, comments);

     return false;

}

***************************************

function _dialogReturnValueCallback(dialogResult, value){

     if(!dialogResult)return;

     SP.UI.ModalDialog.RefreshPage(dialogResult);

}

***************************************

function _startWorkflow(callback, Comment){

     _dialog.close();

     var waitingDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose('Your action is in progress...', "Please wait while the action is being completed.");

     var $request = new Sys.Net.WebRequest();

     $request.set_url((siteUrl||_spPageContextInfo.webServerRelativeUrl.replace(/^\s+|\s+$/gi,''))+"/_vti_bin/NintexWorkflow/Workflow.asmx");

     var $headers = $request.get_headers();

     $headers["Content-Type"]="text/xml; charset=utf-8";

     $headers["SOAPAction"]="http://nintex.com/StartSiteWorkflow";

     $body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

     "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+

          "<soap:Body>" +

               "<StartSiteWorkflow xmlns=\"http://nintex.com\">" +

                    "<workflowName>{0}</workflowName>" +

                    "<associationData>{1}</associationData>" +

               "</StartSiteWorkflow>"+

          "</soap:Body>"+

     "</soap:Envelope>";

     var $data="<Data><Comment>{0}</Comment></Data>";

     var thisArgs =this;

     $request.add_completed(function(executor, args){

          var statusCode = executor.get_statusCode();

          if(statusCode>=200 && statusCode<=399){

               Sys.Debug.trace("Action terminated");

               callback.call(thisArgs, null);

          }else{

               Sys.Debug.trace('Warning : Error (Status code {0})', statusCode);

               alert('Error');

          }

          waitingDialog.close();

     });

     $request.set_body(

          String.format($body, 'YourWorkflowName', SP.Utilities.HttpUtility.htmlEncode(String.format($data, Comment)))

     );

     $request.invoke();

}

0 Kudos
Reply
Not applicable

Re: how to start a workflow on button click using javascript in sharepoint

Jump to solution

Hi Thomas BEDUNEAU​,

Where can I find the Workflow ID ?? tried and failed to find it in my O365

0 Kudos
Reply
Workflow Hero

Re: how to start a workflow on button click using javascript in sharepoint

Jump to solution

On an item, do the same as if you wanted to start a workflow (Properties / WOrkflow) :

Then right click on the name of your workflow /  copy shortcut.

Paste into a text editor, you'll get something like :

javascriptSmiley FrustratedtartWorkflow4('a1174439-e623-47c7-8c75-xxxxxxxxx', '4', '{8587C0F3-6F46-4587-xxxx-xxxxxxxx}')

The first argument is your Wokflow ID !

0 Kudos
Reply
Workflow Hero

Re: how to start a workflow on button click using javascript in sharepoint

Jump to solution

Thomas,

I realize this is an old post for you but I was hoping you could provide me some insight on how you handled this. I placed the code you provided above in "Form Settings","Custom Javascript" (and added the appropriate WF ID).  In the button I want to use I placed "fnStartWorkflow()" in my Client Click heading.  When I click the button, my browser refreshes, but doesn't kick off the WF.  Thoughts?  

Reply
Workflow Hero

Re: how to start a workflow on button click using javascript in sharepoint

Jump to solution

Hi, 

Each time your browser refresh, it's often due to an error in your Custom JavaScript.

To debug, I advise you to remove all of your JavaScript, and past-it part by part, to know which line is crashing your script.

Reply
Workflow Hero

Re: how to start a workflow on button click using javascript in sharepoint

Jump to solution

How to insert ID workflow, for exmaple

...getSubscription('8fabd653-1117-42a8-97ea-20bdd2923fd1');...

With ' ??? and "or () or without, how???

Federico

0 Kudos
Reply
Workflow Hero

Re: how to start a workflow on button click using javascript in sharepoint

Jump to solution

Can you start workflow correctly, with this button????

Fms.

0 Kudos
Reply
Workflow Hero

Re: how to start a workflow on button click using javascript in sharepoint

Jump to solution

I have the next message error: "ReferenceError: showInProgressDialog is not defined", what is???

Thanks

Fms.

0 Kudos
Reply