📜 ⬆️ ⬇️

How to create a workflow and list association programmatically

Purpose of the post



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



Let's agree that the web variable is an SPWeb object containing a list that we need to associate with the workflow.
')

1. Declare the variables to be used.



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 .
  1. 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. 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 .
  3. 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 .
  4. 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 .
  5. 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 .
  6. 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 .
  7. 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 .
  8. 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 .
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 the workflow, it can correspond to 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. // Try to get a list of workflow history
  2. try
  3. {
  4. historyList = web.Lists [ "Workflow History" ];
  5. }
  6. catch (ArgumentException exc)
  7. {
  8. // Create a workflow history list
  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 history and tasks workflow.



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 .

The task list is a regular list inherited from the task list template. If you want to create a specific task list for workflow, do not call it “Tasks”. For example, we can call it “Workflow tasks”.

  1. // Trying to get a list of tasks for workflow
  2. try
  3. {
  4. taskList = web.Lists [ "Workflow Tasks" ];
  5. }
  6. catch (ArgumentException exc)
  7. {
  8. // Create a task list for 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 bind it to the list.



  1. // Enable unsafe updates for the site (web)
  2. web.AllowUnsafeUpdates = true ;
  3. try
  4. {
  5. // Create a workflow association
  6. workflowAssociation = SPWorkflowAssociation.CreateListAssociation (workflowTemplate, workflowAssocName, taskList, historyList);
  7. // Set workflow parameters
  8. workflowAssociation.AllowManual = false ;
  9. workflowAssociation.AutoStartCreate = true ;
  10. workflowAssociation.AutoStartChange = false ;
  11. // Link workflow with the list
  12. myList.AddWorkflowAssociation (workflowAssociation);
  13. // Activate the association
  14. workflowAssociation.Enabled = true ;
  15. }
  16. finally
  17. {
  18. web.AllowUnsafeUpdates = false ;
  19. }

* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/59661/


All Articles