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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
Engineering at Meta
Engineering at Meta
腾讯CDC
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
D
Darknet – Hacking Tools, Hacker News & Cyber Security
WordPress大学
WordPress大学
博客园_首页
I
InfoQ
NISL@THU
NISL@THU
Recorded Future
Recorded Future
Security Latest
Security Latest
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
Webroot Blog
Webroot Blog
Last Week in AI
Last Week in AI
AWS News Blog
AWS News Blog
IT之家
IT之家
S
Schneier on Security
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary
T
Threat Research - Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LangChain Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG
G
GRAHAM CLULEY
O
OpenAI News
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost

博客园 - 范文轩

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等事件来添加以上代码。