How to associate the global resusable workflows to SharePoint list/libraries using JSOM?
Hello SharePointers,
Here are the JSOM script to associate global reusable workflow to a SharePoint list/libraries.
<script type=”text/javascript”>
function AddWorkflow() {
var ctx = SP.ClientContext.get_current();
var site = ctx.get_web();
var templates = ctx.get_web().get_workflowTemplates();
var template = templates.getByName(“My worklows”);
ctx.load(templates);
ctx.load(template);
ctx.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
var info = new SP.Workflow.WorkflowAssociationCreationInformation();
var taskList = ctx.get_web().get_lists().getByTitle(“Workflow Tasks”);
var historyList = ctx.get_web().get_lists().getByTitle(“Workflow History”);
info.set_template(template);
info.set_name(“Test Worfklow”);
info.set_taskList(taskList);
info.set_historyList(historyList);
var list = ctx.get_web().get_lists().getByTitle(“Category”);
var assocs = list.get_workflowAssociations();
var wf=assocs.add(info);
ctx.load(wf);
ctx.executeQueryAsync(Function.createDelegate(this, function (sender, args) {
console.log(“Associated workflow subscription”);
}),
Function.createDelegate(this, function (sender, args) {
console.log(“Error: ” + args.get_message());
}));
}),
Function.createDelegate(this, function (sender, args) {
console.log(“Error: ” + args.get_message());
}));
}
</script>
Happy SharePointing Folks 🙂