Thursday, July 7, 2011

Associating a workflow with a content type programmatically : SharePoint 2010

One of the common requirements in sharepoint projects using workflows is to associate the workflow with a content type so that whenever an item of that content type is created/changed, the workflow starts. Quite often ,we need to do this task programmtically using features/event receivers etc. 

Well, here's the code snippet which specifies how to associate the sharepoint out of the box 'Collect feedback' workflow and the 'Approval' workflow with a content type. You'll need to use the 'Microsoft.SharePoint.Workflow' namespace to be able to run this code.

    string taskListTitle = "Tasks";
    string historyListTitle = "Workflow History";
    string collectFeedbackWorkflowName = "CollectFeedbackWorkflow";
    string approvalWorkflowName = "ApprovalWorkflow";
                                                      
    SPWorkflowTemplate collectFeedbackWorkflowTemplate = null;
    SPWorkflowTemplate approvalWorkflowTemplate = null;


    //Initializing with workflow template IDs
   
    Guid collectFeedbackWorkflowTemplateID = new Guid("3bfb07cb-5c6a-4266-849b-8d6711700409");
    Guid approvalWorkflowTemplateID = new Guid("8ad4d8f0-93a7-4941-9657-cf3706f00409");

    
    //Getting workflow template based on workflow id


       foreach (SPWorkflowTemplate template in this.CurrentWeb.WorkflowTemplates)
            {
              if (template.Id.Equals(collectFeedbackWorkflowTemplateID))
                 {
                   collectFeedbackWorkflowTemplate = template;                                                                       
                 }

                if (template.Id.Equals(approvalWorkflowTemplateID))
                 {
                   approvalWorkflowTemplate = template;
                 }
            }

     //--------------------------- Create collect feedback workflow association.--------------------------------------
      
      SPWorkflowAssociation collectFeedbackWorkflowAssociation = SPWorkflowAssociation.CreateWebContentTypeAssociation(collectFeedbackWorkflowTemplate,
                                                                                       collectFeedbackWorkflowName,
                                                                                       taskListTitle,
                                                                                       historyListTitle);

     //setting to specify that workflow can be started manually

      collectFeedbackWorkflowAssociation.AllowManual = true;

     //setting to specify that manage lists permissions is required for starting a workflow
                 
      if (collectFeedbackWorkflowAssociation.AllowManual)
             {

                SPBasePermissions emptyMask = SPBasePermissions.EmptyMask;
              
                emptyMask |= SPBasePermissions.ManageLists;
               
                collectFeedbackWorkflowAssociation.PermissionsManual = emptyMask;
             }

     //setting to specify that workflow will be automatically started whenever a new item is added
       collectFeedbackWorkflowAssociation.AutoStartCreate = true;

      //setting to specify that workflow will be automatically started whenever an item is changed/edited
       collectFeedbackWorkflowAssociation.AutoStartChange = true;

    
  //Adding workflow association to content type (objContentType is an object of type SPContentType)                     
       objContentType.WorkflowAssociations.Add(collectFeedbackWorkflowAssociation);


     //--------------------------- Create approval workflow association.--------------------------------------

       SPWorkflowAssociation approvalWorkflowAssociation = SPWorkflowAssociation.CreateWebContentTypeAssociation(approvalWorkflowTemplate,
                                                                                       approvalWorkflowName,
                                                                                       taskListTitle,
                                                                                       historyListTitle);

     //setting to specify that workflow can be started manually
       approvalWorkflowAssociation.AllowManual = true;
                           
    //setting to specify that manage lists permissions is required for starting a workflow

       if (approvalWorkflowAssociation.AllowManual)
           {

              SPBasePermissions emptyMask = SPBasePermissions.EmptyMask;
           
              emptyMask |= SPBasePermissions.ManageLists;
              
              approvalWorkflowAssociation.PermissionsManual = emptyMask;
           }

    //setting to specify that workflow will be automatically started whenever a new item is added
       approvalWorkflowAssociation.AutoStartCreate = true;

    //setting to specify that workflow will be automatically started whenever an item is changed/edited
       approvalWorkflowAssociation.AutoStartChange = true;

    
    //Adding workflow association to content type (objContentType is an object of type SPContentType)            
objContentType.WorkflowAssociations.Add(approvalWorkflowAssociation);


Similar code can also be used for the custom workflow templates that we deploy.It is just that in that case we will have to specify the template name for our custom workflow that we need to associate with the content type.