How to associate a worklow to a list programmatically

本文介绍如何通过编程方式将工作流与SharePoint列表进行关联。文中详细展示了使用C#代码检查并创建必要的标准任务列表及工作流历史记录列表的过程,并提供了设置工作流参数的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

How to associate a worklow to a list programmatically

 
 

Goal of this post

This post is a good example of how to programmatically associate a Workflow to a list. 
The workflow can be standard or designed with SharePoint Designer or Visual Studio. 
As a workflow is using a standard tasks list and a specific workflow history list to run properly, the code will check and use or create them if necessary. 
It will also take care of associating the workflow with a list.

Code example

We assume that the variable web is a SPWeb object containing the list we want to associate the workflow with.

1. Declare objects we are going to use

SPList myList = null;                               // List to associate workflow to
string myListName = null;                           // My list name
SPList historyList = null;                          // Workflow history list
SPList taskList = null;                             // Workflow tasks list
string workflowTemplateGuid = null;                 // Workflow template Guid
SPWorkflowTemplate workflowTemplate = null;         // Workflow template
SPWorkflowAssociation workflowAssociation = null;   // Workflow association
string workflowAssocName = null;                    // Workflow association name

2. Init

Be sure to use the internal name of the list. And set the workflow association name, this will appair as workflow name in SharePoint list settings.
myListName = "My list name";
workflowAssocName = "My Workflow";

3. Get Workflow template

If you want to use a custom workflow and know its template GUID, use the GUID string.
workflowTemplateGuid = "BAD855B1-32CE-4bf1-A29E-463678304E1A";
workflowTemplate = web.WorkflowTemplates[new Guid(workflowTemplateGuid)];
Instead you can get the workflow template GUID by using the  GetTemplateByName method.
workflowTemplate = web.WorkflowTemplates.GetTemplateByName(
"Template name",
System.Globalization.CultureInfo.CurrentCulture);

4. Get or create workflow history and tasks lists.

The history list is dedicated to workflows and it's based on the  WorkflowHistory list template. Most of times you'll have to create it.
// Try to get workflow history list
try
{
      historyList = web.Lists["Workflow History"];
}
catch (ArgumentException exc)
{
      // Create workflow history list
      Guid listGuid = web.Lists.Add("Workflow History", "", SPListTemplateType.WorkflowHistory);
      historyList = web.Lists[listGuid];
      historyList.Hidden = true;
      historyList.Update();
}
The tasks list is a common tasks list based on  Tasks list template. If you want to create a specific tasks list for the workflow, don't use "Tasks" title for it. In the example we want to use a dedicated tasks list and will name it "Workflow Tasks".
// Try to get workflow tasks list
try
{
      taskList = web.Lists["Workflow Tasks"];
}
catch (ArgumentException exc)
{
      // Create workflow tasks list
      Guid listGuid = web.Lists.Add("Workflow Tasks", "", SPListTemplateType.Tasks);
      taskList = web.Lists[listGuid];
      taskList.Hidden = true;
      taskList.Update();
}

5. Create workflow association, configure it and associate it to the list.

// Allow unsafe updates on web
web.AllowUnsafeUpdates = true;
try { // Create workflow association workflowAssociation = SPWorkflowAssociation.CreateListAssociation(
workflowTemplate,
workflowAssocName, taskList, historyList); // Set workflow parameters workflowAssociation.AllowManual = false; workflowAssociation.AutoStartCreate = true; workflowAssociation.AutoStartChange = false; // Add workflow association to my list myList.AddWorkflowAssociation(workflowAssociation); // Enable workflow workflowAssociation.Enabled = true; } finally { web.AllowUnsafeUpdates = false; }

http://blogs.prexens.com/Pages/Post.aspx?ID=9

转载于:https://www.cnblogs.com/Areas/archive/2011/11/30/2269543.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值