Home > Blogs > Blog Post

Code Sample : How to associate a SharePoint Workflow to a list programmatically

by Mark Jones | Dec 09, 2011

The following code sample illustrates how to associate a workflow to a list based on the workflow name.

public static void CreateListAssociation(SPWeb web, string listName, 
 string templateName, SPList historyList, 
                                         SPList taskList, string workflowName, 
                                         AssociationType associationType, bool enabled, 
 bool contentTypePushDown, bool allowAsyncManualStart, 
 string description, bool allowManual, 
 string associationData, bool autoStartChange, 
 int autoCleanupDays, bool autoStartCreate, 
 bool lockItem, bool markedForDelete, 
 bool removeIfExist, string contentTypeName)  
{
    LogHelper.LogMethodStart(logger, "SPListHelper", "AssociateWorkFlow");
 
    SPList list = GetList(web, listName);
 
 
    SPWorkflowTemplate workflowTemplate = SPWorkFlowHelper.GetWorkFlowTemplate(templateName, web);
    ValidationHelper.VerifyObjectArgument(workflowTemplate, "workflowTemplate");
 
 if (string.IsNullOrEmpty(workflowName))
        workflowName = workflowTemplate.Name;
    SPWorkflowAssociation association;
 if (associationType == AssociationType.ContentType)
    {
        association = SPWorkflowAssociation.CreateListContentTypeAssociation(workflowTemplate, workflowName, 
                                                                                taskList, historyList);
    }
 else
        association = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, workflowName, 
                                                                            taskList, historyList);
 
 
 
    association.ContentTypePushDown = contentTypePushDown;
    association.AllowAsyncManualStart = allowAsyncManualStart;
    association.Description = description;
    association.AllowManual = allowManual;
    association.AssociationData = associationData;
    association.AutoCleanupDays = autoCleanupDays;
    association.AutoStartChange = autoStartChange;
    association.AutoStartCreate = autoStartCreate;
    association.LockItem = lockItem;
    association.MarkedForDelete = markedForDelete;
 
 
 foreach (SPWorkflowAssociation workflowAssociation in list.WorkflowAssociations)
    {
 if (workflowAssociation.Name == workflowName)
        {
 if (removeIfExist)
            {
                list.WorkflowAssociations.Remove(workflowAssociation);
            }
 else
            {
                LogHelper.LogWarning(logger, "SPListHelper", 
 "Unable to associate workflow as it's already " +
 "associated and RemoveIfExist is false");
 return;
                }
 
 break;
        }
    }
 
 if (associationType == AssociationType.List)
    {
        list.WorkflowAssociations.Add(association);
        association.Enabled = enabled;
    }
 else
    {
        ValidationHelper.VerifyStringArgument(contentTypeName, "contentTypeName");
 bool contentTypeFound = false;
 foreach (SPContentType contentType in list.ContentTypes)
        {
 if (contentType.Name.ToLower() == contentTypeName.ToLower())
            {
                contentType.WorkflowAssociations.Add(association);
                association.Enabled = enabled;
                contentTypeFound = true;
 break;
            }
        }
 
 if (! contentTypeFound)
            LogHelper.LogWarning(logger, 
 "Cannot find this content type '" + contentTypeName + 
 "' on this list : '" + list.Title + "'");
    }
 
 
 
    LogHelper.LogMethodEnd(logger, "SPListHelper", "AssociateWorkFlow");
}

Compliance using DocRead

DocRead logoDocRead for SharePoint can help you manage policy compliance by:

  • Targeting documents or policies at specific groups of users
  • Allowing a specific amount of time for users to confirm agreement 
  • Sending email reminders when policy compliance is overdue
  • Users self-certify that they have read and fully understood the policy details
  • Securely storing records of confirmed policy acceptance
  • Monitoring the user acceptance of policies via a reporting suite
  • Providing detailed reading reports and statistics
  • Report drill through to show who has not accepted the policy
  • Automatically sending historic compliance tasks and policies to new users when they are added to a group
  • Bringing policy compliance requests immediately to users attention when they log on

DocRead is simple to install and configure. It seamlessly integrates with SharePoint and can be added to any existing SharePoint site.

To find out more, visit the DocRead product site.


blog comments powered by Disqus