How to create a workflow and list association programmatically
- Transfer
Post purpose
This post is intended to demonstrate how to perform a workflow and list association programmatically. Workflow can be standard, or created in Visual Studio. As for workflow, it will use standard task lists and a workflow history list.
Examples
We agree that the web variable is an SPWeb object that contains the list that we need to associate with workflow.
1. Declare the variables that we will use
- SPList myList = null; // Лист для ассоциации с workflow
- string myListName = null; // название нашего спика
- SPList historyList = null; // список истории workflow
- SPList taskList = null; // список задач workflow
- string workflowTemplateGuid = null; // Guid шаблона worklfow
- SPWorkflowTemplate workflowTemplate = null; // шаблон Workflow
- SPWorkflowAssociation workflowAssociation = null; // ассоциация с workflow
- string workflowAssocName = null; // имя ассоциации с workflow
* This source code was highlighted with Source Code Highlighter.
2. Initialization
Be sure to use the internal name of the list. And set the name of the association with workflow, it can match the name of the corresponding workflow.
- myListName = "My list name";
- workflowAssocName = "My Workflow";
* This source code was highlighted with Source Code Highlighter.
3. Get a workflow template
If you want to use a custom workflow and know the GUID of its template, use it.
- workflowTemplateGuid = "BAD855B1-32CE-4bf1-A29E-463678304E1A";
- workflowTemplate = web.WorkflowTemplates[new Guid(workflowTemplateGuid)];
* This source code was highlighted with Source Code Highlighter.
You can also get the template GUID using the GetTemplateByName method.
- // Пытаемся получить список истории workflow
- try
- {
- historyList = web.Lists["Workflow History"];
- }
- catch (ArgumentException exc)
- {
- // Создаем список истории workflow
- Guid listGuid = web.Lists.Add("Workflow History", "", SPListTemplateType.WorkflowHistory);
- historyList = web.Lists[listGuid];
- historyList.Hidden = true;
- historyList.Update();
- }
* This source code was highlighted with Source Code Highlighter.
4. Get or create lists of workflow history and tasks.
The history list used by workflow must be inherited from the WorkflowHistory list template. In most cases, you do not need to create it.
- workflowTemplate = web.WorkflowTemplates.GetTemplateByName( "Template name", System.Globalization.CultureInfo.CurrentCulture);
* This source code was highlighted with Source Code Highlighter.
A task list is a regular list inherited from the task list template. If you want to create a specific task list for workflow, you should not call it “Tasks”. For example, we can call it “Workflow tasks”.
- // Пытаемся получить список задач для workflow
- try
- {
- taskList = web.Lists["Workflow Tasks"];
- }
- catch (ArgumentException exc)
- {
- // Создаём список задач для workflow
- Guid listGuid = web.Lists.Add("Workflow Tasks", "", SPListTemplateType.Tasks);
- taskList = web.Lists[listGuid];
- taskList.Hidden = true;
- taskList.Update();
- }
* This source code was highlighted with Source Code Highlighter.
5. Create an association with workflow, configure it and attach it to the list.
- // Включаем небезопасные обновления для узла (web)
- web.AllowUnsafeUpdates = true;
- try
- {
- // Создаем ассоциацию workflow
- workflowAssociation = SPWorkflowAssociation.CreateListAssociation( workflowTemplate, workflowAssocName, taskList, historyList);
-
- // Устанавливаем параметры workflow
- workflowAssociation.AllowManual = false;
- workflowAssociation.AutoStartCreate = true;
- workflowAssociation.AutoStartChange = false;
-
- // Связываем workflow со списком
- myList.AddWorkflowAssociation(workflowAssociation);
-
- // Активируем ассоциацию
- workflowAssociation.Enabled = true;
- }
- finally
- {
- web.AllowUnsafeUpdates = false;
- }
* This source code was highlighted with Source Code Highlighter.