惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
美团技术团队
量子位
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
小众软件
小众软件
博客园 - 司徒正美
罗磊的独立博客
云风的 BLOG
云风的 BLOG
B
Blog RSS Feed
博客园 - 聂微东

博客园 - 范文轩

Sharepoint中的Feature Stapling功能 SharePoint 2010中的WebProvisioned Event Handler 如何向列表中添加数据值(开发篇补充REST) 如何向列表中添加数据值(开发篇) 如何向列表中添加数据值(管理员篇) 在SharePoint 2010中动态加载Visio Web Part InfoPath 2010调用REST的一个小应用 SharePoint 2010 WSP包部署过程中究竟发生什么? 如何查看SharePoint 2010的CU版本 SharePoint 2010多语言包的安装 在SharePoint 2010中使用Linq时候,请注意特殊字符 自定义ASP.NET WebApplication中调用SharePoint2010的对象 在Infopath 2010中调用Web Service 谈谈SharePoint 2010的客户端对象模型的性能问题 给Document Set里面添加文件夹 给Chart Web Part 添加过滤功能 SharePoint 2010的Form认证的用户注册功能 SharePoint 调查列表的自定义错误页面 Reporting Services 2008 and SharePoint 2010
使用编程的方式来启动SharePoint的工作流
范文轩 · 2011-06-09 · via 博客园 - 范文轩

工作流是SharePoint很重要的一部分。我写的这篇文章是通过编程的方式启动工作流。一共分两个部分:启动无参数和有参数的工作流。

参考示例:http://www.sharepointkings.com/2008/09/how-to-pass-parameters-to-workflow.html

首先,通过SharePoint Designer 2010针对一个文档库设计两个工作流:

Name Description
MyWorkflow 没有参数
MyInitialDataWF 三个参数:YourName(string); YourAge(Int); YourDate(DateTime)

首先看一下无参数工作流。

这个比较简单,直接使用SPWorkflowManager.StartWorkflow Method(http://msdn.microsoft.com/en-us/library/ms467505.aspx)来启动工作流,代码如下:

public void StartWorkflow(SPWeb web)
{
    SPList testDocLib = web.GetList("TestDocLib");
    SPListItem wfItem = testDocLib.GetItemById(2);

    SPWorkflowAssociationCollection wfAssociationColls = testDocLib.WorkflowAssociations;
    CultureInfo ci=CultureInfo.CurrentCulture;

    SPWorkflowAssociation spWFAss =
        wfAssociationColls.GetAssociationByName("MyWorkflow", ci);
    Console.WriteLine(spWFAss.Name);
    web.Site.WorkflowManager.StartWorkflow(wfItem, spWFAss, spWFAss.AssociationData, true);         

}

接下来看一下启动带参数的工作流。这里唯一一个注意的地方就是这个参数是需要我们进行序列化的。

我们需要定义一个参数类:

[Serializable()]
    public class WFParameters
    {
        public string YourName { get;set;}
        public int YourAge{get;set;}
        public DateTime YourDate { get; set; }

        public string GetInitXmlString(WFParameters objParameters)
        {
            WFParameters wfData = new WFParameters();
            wfData.YourName = objParameters.YourName;
            wfData.YourAge = objParameters.YourAge;
            wfData.YourDate = objParameters.YourDate;

            using (MemoryStream stream = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(
                    typeof(WFParameters));
                serializer.Serialize(stream, wfData);
                stream.Position = 0;
                byte[] bytes = new byte [stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                return Encoding.UTF8.GetString(bytes);
            }
        }

    }

然后我们来传递这个参数给SPWorkflowAssociation.AssociationData

代码如下:

public void StartWorkflow(SPWeb web)
{
    SPList testDocLib = web.GetList("TestDocLib");
    SPListItem wfItem = testDocLib.GetItemById(2);

    SPWorkflowAssociationCollection wfAssociationColls = testDocLib.WorkflowAssociations;
    CultureInfo ci=CultureInfo.CurrentCulture;

    SPWorkflowAssociation spWFInitialAss =
       wfAssociationColls.GetAssociationByName("MyInitialDataWF", ci);

    WFParameters objParameters = new WFParameters();
    objParameters.YourName = "Jim";
    objParameters.YourAge = 20;
    objParameters.YourDate = DateTime.Parse("6/1/2009");
    string serializedParameters = objParameters.GetInitXmlString(objParameters);

    Console.WriteLine(serializedParameters);
    spWFInitialAss.AssociationData=serializedParameters;
    web.Site.WorkflowManager.StartWorkflow(
        wfItem, spWFInitialAss, spWFInitialAss.AssociationData, true);
}

我们如果跟踪SPWorkflowAssociation.AssociationData这个参数,会发现是一个XML格式的字符串。如下:

<?xml version="1.0"?>
<WFParameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="h
ttp://www.w3.org/2001/XMLSchema">
  <YourName>Jim</YourName>
  <YourAge>20</YourAge>
  <YourDate>2009-06-01T00:00:00</YourDate>
</WFParameters>

代码比较简单,没有加特别的说明。

拓展:在工作中,有时候我们会需要根据某一列的值发生变化时来启动工作流,这个时候的解决方案是通过Event Handler的方式来做逻辑的判断,然后启动工作流。这种时候,我们只需要在ItemUpdated等事件来添加以上代码。