Hi,
It's very frequently required to attach/associate workflow to list or web programmatically. For a workflow a history list and task list is required.
History list is use to log activity/error occurred during workflow running span.
Task list is use to create and assign task for some approval etc. to user.
Workflow Association:- The binding of any workflow template to list or web is called workflow association.
Step. 1 Create a workflow history list.
public void CreateHistoryList(){
using(SPSite site = new SPSite("Your web url")){
using(SPWeb web = site.OpenWeb()){
web.Lists.Add("YourHistoryListTitle","ListDescription",SPListTemplateType.WorkflowHistory);
}
}
}
Step. 2 Create a Workflow Task list.
public void CreateTaskList(){
using(SPSite site = new SPSite("Your web url")){
using(SPWeb web = site.OpenWeb()){
web.Lists.Add("YourTaskListTitle","ListDescription",SPListTemplateType.Tasks);
}
}
}
Step. 3 Associate workflow with web ( In case of Site Workflow)
public void AddWorkflowAssociation(){childWeb.AllowUnsafeUpdates = true;
using(SPSite site = new SPSite("Your web url")){
using(SPWeb web = site.OpenWeb()){
//Add workflow association
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName("YourWorkflowTemplateName", new CultureInfo(1033));
SPWorkflowAssociation association = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, "Workflow Association Name",
web.Lists["TaskListTitle"], web.Lists["HistoryList"]);
//configures workflow assocition options
association.AllowManual = true;
association.AutoStartCreate = false;
association.AutoStartChange = false;
association.AllowAsyncManualStart = false;
web.WorkflowAssociations.Add(association);
web.Update();
}
}
}
Step. 4 Associate workflow with web ( In case of List Workflow)
public void AddWorkflowAssociation(){childWeb.AllowUnsafeUpdates = true;
using(SPSite site = new SPSite("Your web url")){
using(SPWeb web = site.OpenWeb()){
//Add workflow association
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName("YourWorkflowTemplateName", new CultureInfo(1033));
SPWorkflowAssociation association = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, "Workflow Association Name",
web.Lists["TaskListTitle"], web.Lists["HistoryList"]);
//configures workflow assocition options
association.AllowManual = true;
association.AutoStartCreate = false;
association.AutoStartChange = false;
association.AllowAsyncManualStart = false;
SPList list = web.Lists.TryGetList("YourListTitle");
list.WorkflowAssociations.Add(association);
web.Update();
}
}
}
Now your workflow is associated with either list or web.
It's very frequently required to attach/associate workflow to list or web programmatically. For a workflow a history list and task list is required.
History list is use to log activity/error occurred during workflow running span.
Task list is use to create and assign task for some approval etc. to user.
Workflow Association:- The binding of any workflow template to list or web is called workflow association.
Step. 1 Create a workflow history list.
public void CreateHistoryList(){
using(SPSite site = new SPSite("Your web url")){
using(SPWeb web = site.OpenWeb()){
web.Lists.Add("YourHistoryListTitle","ListDescription",SPListTemplateType.WorkflowHistory);
}
}
}
Step. 2 Create a Workflow Task list.
public void CreateTaskList(){
using(SPSite site = new SPSite("Your web url")){
using(SPWeb web = site.OpenWeb()){
web.Lists.Add("YourTaskListTitle","ListDescription",SPListTemplateType.Tasks);
}
}
}
Step. 3 Associate workflow with web ( In case of Site Workflow)
public void AddWorkflowAssociation(){childWeb.AllowUnsafeUpdates = true;
using(SPSite site = new SPSite("Your web url")){
using(SPWeb web = site.OpenWeb()){
//Add workflow association
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName("YourWorkflowTemplateName", new CultureInfo(1033));
SPWorkflowAssociation association = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, "Workflow Association Name",
web.Lists["TaskListTitle"], web.Lists["HistoryList"]);
//configures workflow assocition options
association.AllowManual = true;
association.AutoStartCreate = false;
association.AutoStartChange = false;
association.AllowAsyncManualStart = false;
web.WorkflowAssociations.Add(association);
web.Update();
}
}
}
Step. 4 Associate workflow with web ( In case of List Workflow)
public void AddWorkflowAssociation(){childWeb.AllowUnsafeUpdates = true;
using(SPSite site = new SPSite("Your web url")){
using(SPWeb web = site.OpenWeb()){
//Add workflow association
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName("YourWorkflowTemplateName", new CultureInfo(1033));
SPWorkflowAssociation association = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, "Workflow Association Name",
web.Lists["TaskListTitle"], web.Lists["HistoryList"]);
//configures workflow assocition options
association.AllowManual = true;
association.AutoStartCreate = false;
association.AutoStartChange = false;
association.AllowAsyncManualStart = false;
SPList list = web.Lists.TryGetList("YourListTitle");
list.WorkflowAssociations.Add(association);
web.Update();
}
}
}
Now your workflow is associated with either list or web.
No comments:
Post a Comment