Due to the requirement of application we were not allowed to run the List workflow on ItemCreated event or ItemChanged Event. So i have decided to go for a manual start of workflow.
But we cannot ask user to go the list select the item and start the workflow
So i have decided to start the workflow using the code on a button click for current list item.
For this we need the following Items
But we cannot ask user to go the list select the item and start the workflow
So i have decided to start the workflow using the code on a button click for current list item.
For this we need the following Items
- List GUID
- Workflow Subscription GUID
- Get the Current Subscription
- Kick Off the Workflow
How to get the List GUID
function getListGUID() {
var dfdcpname = $.Deferred(function () {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var PROLIST = $("#hfProList").val(); //List Name
var oList = web.get_lists().getByTitle(PROLIST);
context.load(oList);
context.executeQueryAsync(
function () {
dfdcpname.resolve(oList.get_id());
},
function (sender, args) {
dfdcpname.reject(args.get_message());
});
});
return dfdcpname.promise();
}
How to get the Workflow Subscription ID
function GetWorkflowid() {
var dfdcpname = $.Deferred(function () {
var context = SP.ClientContext.get_current();
var web = context.get_web();
var sMgr = new SP.WorkflowServices.WorkflowServicesManager(context, web);
var wDefs = sMgr.getWorkflowDeploymentService().enumerateDefinitions();
context.load(wDefs);
context.executeQueryAsync(
function () {
var e = wDefs.getEnumerator();
var subscriptionId = '';
while (e.moveNext()) {
var def = e.get_current();
var workflowname = def.get_displayName();
if (workflowname == 'Project Approval') {
subscriptionId = def.get_id();
break;
//StartWorkflow(subscriptionId, itemid);
}
}
dfdcpname.resolve(subscriptionId);
},
function (sender, args) {
dfdcpname.reject(args.get_message());
});
});
return dfdcpname.promise();
}
Get the Current Subscription of Workflow
function StartWorkflow(subscriptionId, itemId) {
var dfdcpname = $.Deferred(function () {
var ctx = SP.ClientContext.get_current();
var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());
var subscription = wfManager.getWorkflowSubscriptionService().enumerateSubscriptionsByDefinition(subscriptionId);
ctx.load(subscription);
ctx.executeQueryAsync(
function (sender, args) {
var subsEnum = subscription.getEnumerator();
var sub = null;
while (subsEnum.moveNext()) {
sub = subsEnum.get_current();
console.log('Subscription: ' + sub.get_name() + ', id: ' + sub.get_id());
dfdcpname.resolve(sub);
break;
}
dfdcpname.resolve(sub);
},
function (sender, args) {
dfdcpname.reject(args.get_message());
}
);
});
return dfdcpname.promise();
}
Kick off the workflow on a List item
function StartWorkflowBySubscription(sub, itemId) {
var dfdcpname = $.Deferred(function () {
var ctx = SP.ClientContext.get_current();
var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());
var initiationParams = {};
wfManager.getWorkflowInstanceService().startWorkflowOnListItem(sub, itemId, initiationParams);
ctx.executeQueryAsync(function (sender, args) {
dfdcpname.resolve('Workflow Started');
}, function (sender, args) {
dfdcpname.reject(args.get_message());
});
});
return dfdcpname.promise();
}
Register JS required for the Workflow
function StartWorkflowMain() {
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", StartListWorkflow);
})
}
Start List Workflow function
function StartListWorkflow() {
GetItemIdByProjectName().done(function (ItemId) {
GetWorkflowid().done(function (subscriptionId) {
StartWorkflow(subscriptionId, ItemId).done(function (sub) {
StartWorkflowBySubscription(sub, ItemId).done(function (messg) {
console.log(messg);
});
});
});
});
}
No comments:
Post a Comment