Monday, April 4, 2016

C# Associating a Workflow Manager Workflow to a SharePoint list

Workflow Manager workflows are different to the prior versions as it follows a subscription based model for creating associations for the Workflow Manager. In this article I am planning to showcase the code snippet which allows associating a workflow to a SharePoint list as it is not much famous and mentioned at many places.


WorkflowServicesManager workflowServicesManager = new WorkflowServicesManager(currentWeb);
            WorkflowDeploymentService workflowDeploymentService = workflowServicesManager.GetWorkflowDeploymentService();


WorkflowDefinitionCollection workflowDefinitions = workflowDeploymentService.EnumerateDefinitions(true);
foreach (WorkflowDefinition workflowDefinition in workflowDefinitions)
            {
                if (workflowDefinition.DisplayName.Equals(TaskEscalationWorkflowAssociationEventReceiver.TaskEscalationWorkflowDisplayName))

                {
this.AssociateListWorkflow(
                        taskList,
                        historyList,
                        eventTypes,
                        listName,
                        workflowServicesManager,
                        workflowDefinition,

                        0);
}
}

private void AssociateListWorkflow(
            SPList taskList,
            SPList historyList,
            List<string> eventTypes,
            SPList list,
            WorkflowServicesManager workflowServicesManager,
            WorkflowDefinition workflowDefinition,
            int associationIndex)
        {
            WorkflowSubscription workflowSubscription = new WorkflowSubscription();
            workflowSubscription.DefinitionId = workflowDefinition.Id;
            workflowSubscription.Enabled = true;
            workflowSubscription.EventSourceId = list.ID;
            workflowSubscription.Name = workflowDefinition.DisplayName;

            workflowSubscription.EventTypes = eventTypes;
            workflowSubscription.SetProperty(
                TaskEscalationWorkflowAssociationEventReceiver.TaskListId,
                workflowTaskList.ID.ToString());
            workflowSubscription.SetProperty(
                TaskEscalationWorkflowAssociationEventReceiver.HistoryListId,
                workflowHistoryList.ID.ToString());
            workflowSubscription.SetProperty(
                TaskEscalationWorkflowAssociationEventReceiver.FormData,
                string.Empty);

            WorkflowSubscriptionService workflowSubscriptionService = workflowServicesManager.GetWorkflowSubscriptionService();
            if (workflowSubscriptionService.EnumerateSubscriptionsByList(list.ID).Count == 0)
            {
                workflowSubscriptionService.PublishSubscriptionForList(workflowSubscription, list.ID);
            }

        }
            


Normally we put this piece of code for execution for a feature receiver so that gets executed upon feature activation.  You can set the event types if you pass the eventTypes variable as follows.

List<string> eventTypes = new List<string>() { "WorkflowStart", "ItemAdded" };

This will make sure the workflow can be started manually as well as automatically starts when an item is added. You can make it start on update as well depending on the requirement.

No comments:

Post a Comment