*This is for SharePoint Online
Sometimes there are requirements from business where they need a button in list view against each item and once you click on it, it should run a workflow for that particular list item.
Make sure to make below changes before using this code.
- Change the name of the workflow to start mentioned in variable "WorkflowName",
- replace the siteURL
- Change the column name against which you want to show the button. I am overriding default 'Edit' button
/* Code Starts*/
/*Reference Files*/
<script src="https://
siteURL/SiteAssets/jquery-3.2.1.min.js"></script>
<script src="
https://siteURL/_layouts/15/SP.Runtime.js"></script>
<script src="
https://siteURL/_layouts/15/sp.workflowservices.js"></script>
<script src="
https://siteURL/_layouts/15/wfutility.js"></script>
/**/
var itemID = '';
var ItemGUID = '';
var WFSubscriptionID = '';
var WorkflowName = 'Meetings WF';
function getItems(ListTitle, Id) {
var d = $.Deferred();
var url = "/_api/web/lists/getByTitle('" + ListTitle + "')/getItemById(" + Id + ")";
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + url,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
"content-Type": "application/json;odata=verbose"
},
success: function(data) {
console.log(data)
d.resolve(data);
},
error: function() {
d.reject('bad request')
}
});
return d.promise();
}
function GetGUID(z, Id) {
itemID = Id;
getItems(z, Id).then(function(data) {
console.log(data.d.GUID);
ItemGUID = data.d.GUID;
GetWFSubscriptionID(ctx.listName, itemID, ItemGUID);
})
}
function StartWorkflowJsLink(overrideCtx) {
//overrideCtx.listName = "{88E4FF8A-94E8-40AA-89BE-79A98B1CB370}";
return '<input type="button" value="Start Workflow" onclick="GetGUID(' + "'" + overrideCtx.ListTitle + "'" + ',' + "'" + overrideCtx.CurrentItem.ID + "'" + ')"/>';
}
function registerListRenderer() {
var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.Templates.Fields = {
'Edit': {
'View': StartWorkflowJsLink
}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
}
ExecuteOrDelayUntilScriptLoaded(registerListRenderer, 'clienttemplates.js');
function GetWFSubscriptionID(ListID, ItemID, ItemGUID) {
showInProgressDialog();
var listGuid = "88e4ff8a-94e8-40aa-89be-79a98b1cb370";
var context = SP.ClientContext.get_current();
var web = context.get_web();
var sMgr = new SP.WorkflowServices.WorkflowServicesManager(context, web);
var sservice = sMgr.getWorkflowSubscriptionService();
var ssubs = sservice.enumerateSubscriptionsByList(listGuid);
context.load(ssubs);
context.executeQueryAsync(
function() {
var e = ssubs.getEnumerator();
while (e.moveNext()) {
var c = e.get_current();
if (c.get_name() === WorkflowName) {
var subId = c.get_id();
WFSubscriptionID = subId;
StartWorkflow4(WFSubscriptionID, itemID, ItemGUID);
}
//alert("Name :" + c.get_name() + " sID: " + c.get_id());
};
},
function() {
});
}
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) {
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[i]] = subscription.get_propertyDefinitions()[assocParams[i]];
}
}
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();*/
window.location.href = window.location.href;
},
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);
}
/*Code Ends*/
You would be able to see "Start Workflow" button in the overridden column as shown below.
Clicking on the button will run that workflow on the corresponding item.