使用编程的方式来启动SharePoint的工作流 并传入参数

本文介绍了如何在SharePoint中定义类、使用XML序列化将参数传递给工作流,并在工作流中获取这些参数。通过事件处理器实现动态执行工作流,演示了工作流参数的传递和获取过程。

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


 (1) Define one class with properties

using System.Xml.Serialization;
using System.Xml;
using System.IO;

[Serializable()]
public class WorkflowParameters
{
private string _ZipCode = default(string);


public string ZipCode
{
get { return _ZipCode; }
set { _ZipCode = value; }
}


private string _CountryCode = default(string);

public string CountryCode
{
get { return _CountryCode; }
set { _CountryCode = value; }
}

public string getInitXmlString(WorkflowParameters objParams)
{

WorkflowParameters data = new WorkflowParameters();

data._CountryCode = objParams._CountryCode;

data._ZipCode = objParams._ZipCode;


using (MemoryStream stream = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(WorkflowParameters));

serializer.Serialize(stream, data);

stream.Position = 0;

byte[] bytes = new byte[stream.Length];

stream.Read(bytes, 0, bytes.Length);

return Encoding.UTF8.GetString(bytes);

}
}

}


 

Here as you can see we have defined two properties ZipCode and CountryCode as an example. you make paramters accordingly to your need.

(2) Next, step is to dynamically execute the workflow, for this code you can refer post 1. I am working with event handler of a list, so here is a code which demonstrate the idea about executing workflow from event handler. change this code according to the code where you writing it.

SPWorkflowManager objWorkflowManager = null;
SPWorkflowAssociationCollection objWorkflowAssociationCollection = null;

SPListItem item = properties.ListItem;

objWorkflowManager = item.Web.Site.WorkflowManager;

objWorkflowAssociationCollection = item.ParentList.WorkflowAssociations;

foreach (SPWorkflowAssociation objWorkflowAssociation in objWorkflowAssociationCollection)
{
//Find the GUID for the workflow association 

WorkflowParameters objParams = new WorkflowParameters();

objParams.ZipCode = "12345";
objParams.CountryCode = "USA";

string strSerializedParams = objParams.getInitXmlString(objParams);

objWorkflowAssociation.AssociationData = strSerializedParams;

if (String.Compare(objWorkflowAssociation.BaseId.ToString("B"),
"{workflow_ID}", true) == 0)
{
objWorkflowManager.StartWorkflow(item, objWorkflowAssociation,
objWorkflowAssociation.AssociationData, true);

}
} 


As you can see here we have created an object of our previous defined class which is serializable. we set the properties and passed it to a public method of the same class and then we get the string as in the form of XML, which is then assigned to the AssociationData of workflowassociation which is passed to a workflow.

(3) Now you need to retrive these data in the workflow. Now you have to go to a Workflow Code. Let us assume that we fetch these data in Workflow Invoke Activity.

 

//Assume that this is the activity that executes when workflow triggers


private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
{

//Fetch with workflow properties InitiationData property which gets the
//Parameters passed to it.

string strParametrsPassed = workflowProperties.InitiationData;

XmlSerializer xs = new XmlSerializer(typeof(WorkflowParameters));
XmlTextReader xtr = new XmlTextReader(new System.IO.StringReader(workflowProperties.InitiationData));
WorkflowParameters init = (WorkflowParameters)xs.Deserialize(xtr);
string zipcode = init.ZipCode;

//Now this string returns you string in XML format which looks like this,

//<?xml version="1.0"?>
//<WorkflowParameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-//instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
// <ZipCode>12345</ZipCode>
// <CountryCode>USA</CountryCode>
//</WorkflowParameters>

}

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

http://www.cnblogs.com/fanwenxuan/archive/2011/06/09/2076829.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值