How to create a workflow and list association programmatically

Original author: David HOCQUET
  • 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



  1. SPList myList = null;                              // Лист для ассоциации с workflow
  2. string myListName = null;                          // название нашего спика
  3. SPList historyList = null;                         // список истории workflow
  4. SPList taskList = null;                             // список задач workflow
  5. string workflowTemplateGuid = null;                 // Guid шаблона worklfow
  6. SPWorkflowTemplate workflowTemplate = null;         // шаблон Workflow
  7. SPWorkflowAssociation workflowAssociation = null;  // ассоциация с workflow
  8. 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.

  1. myListName = "My list name";
  2. 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.

  1. workflowTemplateGuid = "BAD855B1-32CE-4bf1-A29E-463678304E1A";
  2. 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.

  1. // Пытаемся получить список истории workflow
  2. try
  3. {
  4.      historyList = web.Lists["Workflow History"];
  5. }
  6. catch (ArgumentException exc)
  7. {
  8.      // Создаем список истории workflow
  9.      Guid listGuid = web.Lists.Add("Workflow History", "", SPListTemplateType.WorkflowHistory);
  10.      historyList = web.Lists[listGuid];
  11.      historyList.Hidden = true;
  12.      historyList.Update();
  13. }

* 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.

  1. 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”.

  1. // Пытаемся получить список задач для workflow
  2. try
  3. {
  4.      taskList = web.Lists["Workflow Tasks"];
  5. }
  6. catch (ArgumentException exc)
  7. {
  8.      // Создаём список задач для workflow
  9.      Guid listGuid = web.Lists.Add("Workflow Tasks", "", SPListTemplateType.Tasks);
  10.      taskList = web.Lists[listGuid];
  11.      taskList.Hidden = true;
  12.      taskList.Update();
  13. }

* This source code was highlighted with Source Code Highlighter.


5. Create an association with workflow, configure it and attach it to the list.



  1. // Включаем небезопасные обновления для узла (web)
  2. web.AllowUnsafeUpdates = true;
  3. try
  4. {
  5.         // Создаем ассоциацию workflow
  6.         workflowAssociation = SPWorkflowAssociation.CreateListAssociation(                workflowTemplate,                workflowAssocName, taskList, historyList);
  7.  
  8.         // Устанавливаем параметры workflow
  9.         workflowAssociation.AllowManual = false;
  10.         workflowAssociation.AutoStartCreate = true;
  11.         workflowAssociation.AutoStartChange = false;
  12.  
  13.         // Связываем workflow со списком
  14.         myList.AddWorkflowAssociation(workflowAssociation);
  15.  
  16.         // Активируем ассоциацию
  17.         workflowAssociation.Enabled = true;
  18. }
  19. finally
  20. {
  21.         web.AllowUnsafeUpdates = false;
  22. }

* This source code was highlighted with Source Code Highlighter.

Also popular now: