I have a request to start workflow using client side code. Below is the code that fix my issue:
Code sample:
<button type="button" onclick="Startworkflow(); return false;">Start Workflow</button>
<script src="https://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
function Startworkflow()
{ LoadScripts(); }
function LoadScripts(){
SP.SOD.executeFunc("sp.js", "SP.ClientContext" , function(){
SP.SOD.registerSod('sp.workflowservices.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.workflowservices.js'));
SP.SOD.executeFunc('sp.workflowservices.js', "SP.WorkflowServices.WorkflowServicesManager", StartWorkflow);
})
}
//Subscription id - Workflow subscription id
//list item id for which to start workflow. If site workflow, then send null for itemId
function StartWorkflow() {
var subscriptionId = "f5fa7d47-52c0-48af-9728-acfad55731d2";
var itemId = "";
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var wfManager = new SP.WorkflowServices.WorkflowServicesManager(ctx, web);
var subscription = wfManager.getWorkflowSubscriptionService().getSubscription(subscriptionId);
ctx.load(subscription, 'PropertyDefinitions');
ctx.executeQueryAsync(
function (sender, args) {
var params= new Object();
//Find initiation data to be passed to workflow.
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) {
console.log("workflow start successfully");
},
function (sender, args) {
alert('Failed to run workflow');
}
);
},
function (sender, args) {
alert('Failed to run workflow');
}
);
}
As I have a site workflow, I am passing "itemId" parameter value null. You can pass your item id there.
Reference: http://ranaictiu-technicalblog.blogspot.in/2013/06/sharepoint-2013-start-workflow-with.html
No comments:
Post a Comment