If you want to pro-grammatically create workflow in SharePoint 2013 use following steps:
1. First create workflow in SharePoint Designer 2013.
This will save workflow.wsp in SiteAsstes. Download that.
3. Change the extension of workflow.wsp to .cab. This converts file to RAR file. Extract it.
4. Get the file from "Files\wfsvc\87f127f963084cdf884a683bcb76e3e0 ". content of this file is used as xaml in below code.
var ctx = SP.ClientContext.get_current();
var servicesManager = SP.WorkflowServices.WorkflowServicesManager.newObject(ctx, ctx.get_web());
var definition = SP.WorkflowServices.WorkflowDefinition.newObject(ctx, ctx.get_web());
definition.set_xaml(xaml);
definition.set_displayName("Test");
var deploymentService = servicesManager.getWorkflowDeploymentService();
deploymentService.saveDefinition(definition);
ctx.load(definition);
ctx.executeQueryAsync(function () {
deploymentService.publishDefinition(definition.get_id());
ctx.executeQueryAsync(function () {
var subscription = SP.WorkflowServices.WorkflowSubscription.newObject(ctx, ctx.get_web());
subscription.set_name("Test");
subscription.set_enabled(true);
subscription.set_definitionId(definition.get_id());
var targetListId="07E21547-3ACD-4297-909E-6660F443E892";
subscription.set_eventSourceId(targetListId);
subscription.set_eventTypes(["ItemAdded"]);
var subscriptionService = servicesManager.getWorkflowSubscriptionService();
subscriptionService.publishSubscriptionForList(subscription, targetListId);
ctx.executeQueryAsync(function () {
console.log("done");
},function (sender,arg) {
console.log(arg.get_message());
});
});
});

No comments:
Post a Comment