Sharepoint - Can I add a start workflow link to a column in a list view?

Note:

in June 2017, Microsoft disabled the use of JavaScript in a Calculated Column

That means given answers may not apply for newer SharePoint versions

For long explanation and work arounds see:
June 13th 2017 Microsoft blocked handling HTML markup in SharePoint calculated fields - how to get the same functionality back


**Original answer:**

Check out https://www.365csi.nl/vm365com/#/How

It step by step explains how to execute JavaScript from a Calculated Column.

The ID for an item is available on the Table Row, because a Calculated Formula is displayed inside that TR structure;
all you have to do is:

var TR=this;while(TR.tagName!='TR'){TR=TR.parentNode}

to get to that TR and its iid attribute

var ID=TR.iid.split(',')[1]

Note that you can get the List GUID from the current context.

var ctx = new SP.ClientContext.get_current();
var listGuid = SP.ListOperation.Selection.getSelectedList();

The Workflow GUID you have to do some more JavaScript digging for, but hardcoding is the easiest option.

Client Side Rendering

Ofcourse on SharePoint 2013 you can also do this with CSR. But you have to add an extra JS file and JSlinks on every View you want it applied.

In a Calculated Column it just works.

Update #1

This Formula in a Calculated Column set to datatype=Number will get you started;
I haven't tested if the URL from your original post actually starts a workflow.

="<button style=""cursor:pointer;"" onclick=""{"
&"event.preventDefault();"
&"var clientContext=new SP.ClientContext.get_current();"
&"var listGUID=SP.ListOperation.Selection.getSelectedList();"
&"var workflowGUID='{F704E402-6DA6-4C75-AA97-FBC6F2BEA69F}';"
&"var TR=this;while(TR.tagName!='TR'){TR=TR.parentNode}" 
&"var url='https://mysharepointurl/_layouts/15/IniWrkflIP.aspx?List='+listGUID;" 
&"url += '&TemplateID='+workflowGUID;" 
&"url += '&ID='+TR.id.split(',')[1];" 
&"alert(url);"
&"SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog',{url:url});"
&"}"">Collect Signature</button>"

Update #2

Took me a day to wade trhu all the blogs with partial (and bloated) answers out there...

Yes the Workflow GUID changes every time you make a change. So you have to query the Workflow Subscriptions on the List, a matching Name gives you the ID (but you need the 'Subscription' to start a workflow, Not the GUID)
Alas the library sp.workflowservices.js needed for this is not available on the List view page; so some extra scripting is required to load it if needed (only for the first button you click)

This immediatly starts the Workflow, no pages in between

Copy/Paste in a Calculated Column, set the datatype to Number

="<button style=""cursor:pointer;"" onclick=""{"
&"event.preventDefault();"
&"function startWorkflow(itemID, wfName) {"
&"  function __startWorkflow() {"
&"      var ctx = new SP.ClientContext.get_current(),"
&"          wfsManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx,ctx.get_web()),"
&"          wfSubs = wfsManager.getWorkflowSubscriptionService().enumerateSubscriptionsByList(_spPageContextInfo.pageListId);"
&"      ctx.load(wfSubs);"
&"      ctx.executeQueryAsync(function () {"
&"          wfsEnum = wfSubs.getEnumerator();"
&"          while (wfsEnum.moveNext()) {"
&"              var wfSub = wfsEnum.get_current();"
&"              if (wfSub.get_name() === wfName) {"
&"                  wfsManager.getWorkflowInstanceService().startWorkflowOnListItem(wfSub,itemID,new Object());"
&"                  SP.UI.Notify.addNotification('Init Workflow: '+wfName+' on item: '+itemID, false);"
&"              }}});}"
&"  if (!SP.WorkflowServices) {"
&"      var script = document.createElement('script');"
&"      script.src = '/_layouts/15/sp.workflowservices.js';"
&"      script.onload = __startWorkflow;"
&"      document.head.appendChild(script);"
&"  } else {__startWorkflow();}"
&"}"
&"var TR=this;while(TR.tagName!='TR'){TR=TR.parentNode}"    
&"startWorkflow(TR.id.split(',')[1] , 'YOUR_WORKFLOW_TITLE_GOES_HERE');"
&"}"">Collect Signature</button>"

Should work on any site, no need to change URLs, all you need to change is: YOUR_WORKFLOW_TITLE_GOES_HERE
There is no error checking at all, I debugged with a simple Send-Email workflow.

Fun with Calculated Columns

You can apply all your Calculated Formula skills; since Formula contents are beign evaluated on every Item change you are essentially creating a kind of self-modyfing JavaScript code.

&"startWorkflow(TR.id.split(',')[1] , 'YOUR_WORKFLOW_TITLE_GOES_HERE');"

Change it to select different Workflows based on a (task) Status:

&"startWorkflow(TR.id.split(',')[1] , '"
  &IF(Status="Approved","MyUnApproveWF","CollectSignatureWF")
&"');"

ICC IWF


This should be what your looking for: 4 Clicks or 1?