Scenario:
You want to provision a Workflow programatically for a given list/document library
Steps:
01. Create an SPList object that references a list or document library that hosts the items used by the workflow.
02. Create an SPList object that references an approval task list.
03. Create an SPList object that references the Workflow History list.
04. Create an SPWorkflowTemplate object by using the workflow GUID from Workflow.xml.
05. Create an SPWorkflowAssociation object that connects the SPWorkflowTemplate object with the SPList objects for the approval task list and the Workflow History list.
06. Add an SPWorkflowAssociation object to the SPList object you created in the first step.
Code:
public void ProvisionWorkflow(SPWeb web)
{
using (web)
{
//Bind to lists.
SPList approvalsList = web.Lists["Your Approval List Name Goes Here"];
SPList approvalTasksList = web.Lists["Task List"];
SPList workflowHistoryList = web.Lists["Workflow History"];
//WorkflowGUID is the workflow GUID
//from workflow.xml\<Elements>\<Workflow>\[ID].
SPWorkflowTemplate workflowTemplate =
web.WorkflowTemplates[new Guid("WorkflowGUIDWithoutBraces")];
//Create workflow association.
SPWorkflowAssociation workflowAssociation =
SPWorkflowAssociation.CreateListAssociation(workflowTemplate,
workflowTemplate.Description, approvalTasksList,
workflowHistoryList);
//Set workflow options.
workflowAssociation.AllowManual = true;
workflowAssociation.AutoStartChange = true;
workflowAssociation.AutoStartCreate = false;
//Add workflow association.
SPWorkflowAssociation workflowAssociationInList =
approvalsList.AddWorkflowAssociation(workflowAssociation);
}
return;
}
0 comments:
Post a Comment