Monday, July 11, 2016

Associating SharePoint 2013 Workflow to a list

SharePoint 2013 supports Workflow Manager workflows. Which means they totally different with 2010 workflows. In reality what happens is that we publish a subscription for the particular list. Though its been a while SharePoint 2013 has come, I have seen many people struggling without getting this done. So I thought its worth sharing a sample code snippet with an explanation.

To get a detailed explanation, we need to have a little understanding on the following classes. 


WorkflowServiceManager - This is the class used for configuring and upgrading of an installation of a Microsoft Azure Workflow
WorkflowDeploymentService - This class manages workflow definitions and workflow activity authoring.
WorkflowDefinition - Represents a SharePoint workflow definition and includes the workflow properties. 
WorkflowDefinitionCollection - Client object collection of WorkflowDefinition class.
WorkflowSubscription - Class responsible for having subscriptions to the external workflow hosts such as Azure workflows.

WorkflowServicesManager workflowServicesManager = new WorkflowServicesManager(currentWeb);
WorkflowDeploymentService workflowDeploymentService 
= workflowServicesManager.GetWorkflowDeploymentService();
WorkflowDefinitionCollection workflowDefinitions 
= workflowDeploymentService.EnumerateDefinitions(publishedDefinitionsOnly);

// We can loop through the definitions.
foreach (WorkflowDefinition workflowDefinition in workflowDefinitions)
{ 
  if(workflowDefinition.DisplayName.Equals(expectedDefinitionName)
  {
      WorkflowSubscription workflowSubscription = new WorkflowSubscription();
      workflowSubscription.DefinitionId = workflowDefinition.Id;
      workflowSubscription.Enabled = true;
      workflowSubscription.EventSourceId = listGuid;
      workflowSubscription.Name = workflowDefinition.DisplayName;
      // Event types when the workflow need to trigger such as Item Adding, Update and Manual
      workflowSubscription.EventTypes = eventTypes;
      workflowSubscription.SetProperty("TaskListId", taskListID);
      workflowSubscription.SetProperty("HistoryListId", historyListID);
      workflowSubscription.SetProperty("FormData, string.Empty);

      WorkflowSubscriptionService workflowSubscriptionService 
= workflowServicesManager.GetWorkflowSubscriptionService();
      workflowSubscriptionService.PublishSubscriptionForList(workflowSubscription, listGuid);
  }
}

7 comments:

  1. Hi Author,

    May I ask a question?
    I confuse about TaskListId, HistoryListId
    So how to create or initialize these variables

    Thanks

    ReplyDelete
  2. Hello there, just became alert to your blog through Google, and found that it's truly informative. I am gonna watch out for brussels. I'll be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers! outlook 365 login

    ReplyDelete
  3. Virtual office applications are an isolated office collaboration program that assists virtual offices. It allows online employees to get to know task list templates

    ReplyDelete
  4. Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! https://europa-road.eu/hu/kulonleges-gepszallitas-budapest.php

    ReplyDelete
  5. A virtual office is a great way for your small business to save on the cost of renting an office space. If you want to know details about virtual office, check out glasgowvirtualoffices.co.uk.

    ReplyDelete
  6. Excellent post! I wonder if computer software-based workflow automated can run seamlessly with workflow automation with mobile apps. This will bring huge benefits for small & large enterprises.

    ReplyDelete
  7. By default 30-yr fixed-rate loans are displayed in the table below. canada mortgage calculator Despite the long process, securing an advert loan is vital for most businesses. mortgage calculator

    ReplyDelete