다음을 통해 공유


Code snippet to create a Tasks list and associate it with a Parallel Approval workflow in a Pages library

The requirement to write this code was, After export/import, workflow modification for approvers was failing. Approvers were not able to perform the Approval workflow operations and then we tested that if we create a new Tasks list and modify the existing Approval workflow association then everything starts working so we needed a code which can do this operation for whole site collection.

Following code snippet will create a Tasks list with name “MS Tasks” and associate it with a Parallel Approval workflow in a “Pages” library in root site as well as in all the sub sites.

image

    1:  using System;
    2:  using System.Collections.Generic;
    3:  using System.ComponentModel;
    4:  using System.Data;
    5:  using System.Drawing;
    6:  using System.Linq;
    7:  using System.Text;
    8:  using System.Windows.Forms;
    9:  using Microsoft.SharePoint;
   10:  using Microsoft.SharePoint.Publishing;
   11:  using Microsoft.SharePoint.Workflow;
   12:   
   13:  namespace WorkflowAssoc
   14:  {
   15:      public partial class Form1 : Form
   16:      {
   17:          public Form1()
   18:          {
   19:              InitializeComponent();
   20:          }
   21:   
   22:   
   23:          private void button1_Click(object sender, EventArgs e)
   24:          {
   25:              using (SPSite site = new SPSite(textBox1.Text.Trim()))
   26:              {
   27:                  using (SPWeb web = site.OpenWeb())
   28:                  {
   29:                      //executing for root site
   30:                      WorkflowModification(web);
   31:   
   32:                      //executing for all subsites
   33:                      foreach (SPWeb subweb in web.Webs)
   34:                      {
   35:                          WorkflowModification(subweb);
   36:                      }
   37:                  }
   38:              }
   39:   
   40:              MessageBox.Show("Done!");
   41:          }
   42:   
   43:          private void WorkflowModification(SPWeb web)
   44:          {
   45:              bool checkTasks = true;
   46:              bool checkPages = true;
   47:              SPList pages;
   48:              SPWorkflowAssociation WFCol;
   49:              SPList tasks;
   50:   
   51:              //making sure Pages list is available.
   52:              foreach (SPList list in web.Lists)
   53:              {
   54:                  if (list.Title == "Pages")
   55:                  {
   56:                      checkPages = false;
   57:                  }
   58:              }
   59:   
   60:              //making sure Tasks list is not available.
   61:              foreach (SPList list in web.Lists)
   62:              {
   63:                  if (list.Title == "MS Tasks")
   64:                  {
   65:                      checkTasks = false;
   66:                  }
   67:              }
   68:   
   69:              //execute only if Tasks list not available and Pages list is available
   70:              if (checkTasks && !checkPages)
   71:              {
   72:                  //creating a Tasks list
   73:                  web.Lists.Add("MS Tasks", "MS Tasks List", SPListTemplateType.Tasks);
   74:   
   75:                  //getting an object of Pages list
   76:                  pages = web.Lists["Pages"];
   77:   
   78:                  //getting an object of Tasks list and setting it in workflow association
   79:                  tasks = web.Lists["MS Tasks"];
   80:   
   81:                  //getting an object of "Parallel Approval" workflow
   82:                  //this is the only workflow available so first index
   83:                  for (int i = 0; i < pages.WorkflowAssociations.Count; i++)
   84:                  {
   85:                      WFCol = pages.WorkflowAssociations[i];
   86:   
   87:                      //setting up Tasks list 
   88:                      WFCol.SetTaskList(tasks);
   89:                      WFCol.TaskListTitle = "MS Tasks";
   90:   
   91:                      //updating the workflow association
   92:                      pages.UpdateWorkflowAssociation(WFCol);
   93:   
   94:                      //clearing the objects
   95:                      WFCol = null;
   96:                  }
   97:                  tasks = null;
   98:                  pages = null;
   99:              }
  100:          }
  101:      }
  102:  }

image

:: Please note that I would have just uploaded my initial code and you might want to consider proper optimization of the code and disposal of objects properly. I might not have updated the latest code here.