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

来源:互联网 发布:深圳社保积分怎么算法 编辑:程序博客网 时间:2024/06/06 17:55

 (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 triggersprivate 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

原创粉丝点击