转:http://blog.csdn.net/gxiangzi/article/details/8444590
本来想一篇文章把流程开发介绍完的,后来发现实在是太多了,只好分成两部分了。上一篇很简单的介绍了下k2流程的设计和配置,希望大家的流程都已经发布成功了。这部分接着上面讲,主要说一下k2流程api的使用。
我们要在k2web中新建一下几个页面:
- LeaveProcessStartflow.aspx:请假流程发起界面
- LeaveProcessAudit.aspx:请假流程审批界面
- index.aspx:流程列表界面
- MyWorklist.aspx:我的待办流程列表界面
1.首先贴一下流程处理的公共类,很简单就是打开k2的链接:
- using SourceCode.Workflow.Client;
- using SourceCode.Workflow.Management;
- //以上dll请在解决方案里面引用
- namespace K2Web
- {
- public class K2Util
- {
- public K2Util() { }
- public Connection GetK2Connection()
- {
- Connection k2con = new Connection();
- // Connect to Workflow Server
- k2con.Open("SZ-JIANGWX:5252");
- return k2con;
- }
- public WorkflowManagementServer GetK2WorkflowMgr()
- {
- WorkflowManagementServer wms = new WorkflowManagementServer("SZ-JIANGWX", 5555);
- wms.Open();
- return wms;
- }
- }
- }
2.流程发起界面 Startflow.aspx
前台界面我就不贴了,拉三个textbox分别是我们请假流程的三个参数,后面贴一下后台主要代码。
- //提交按钮事件处理程序
- protected void btnSave_Click(object sender, EventArgs e)
- {
- K2Util k2 = new K2Util();
- Connection k2con = k2.GetK2Connection();//打开链接
- string flowname = @"K2WorkflowLeaveProcess";//发布的流程名称
- ProcessInstance pi = k2con.CreateProcessInstance(flowname);
- //流程数据赋值
- pi.DataFields["请假人"].Value = txtOriginator.Text;
- pi.DataFields["请假天数"].Value = txtDays.Text;
- pi.DataFields["请假原因"].Value = txtReason.Text;
- //设置流程标志 我这里是 发起人姓名+流程名称+发起时间
- pi.Folio = txtOriginator.Text+"LeaveProcess" + DateTime.Now.ToString("yyyyMMddHHmm");
- //启动流程
- k2con.StartProcessInstance(pi);
- k2con.Close();//关闭链接
- }
3.流程审批界面 Audit.aspx
前台界面没有太大变化,发起界面的三个textbox换成了label用以展示数据,此外另外加了一个dropdownlist,用以加载审批活动的 Action列表。前台代码如下:
- <table style=" 50%;">
- <tr>
- <td align="right">
- 请假人:
- </td>
- <td align="left">
- <asp:Label ID="txtOriginator" runat="server"></asp:Label>
- </td>
- </tr>
- <tr>
- <td align="right">
- 请假天数:
- </td>
- <td align="left">
- <asp:Label ID="txtDays" runat="server"></asp:Label>天
- </td>
- </tr>
- <tr>
- <td align="right">
- 请假原因:
- </td>
- <td align="left">
- <asp:Label ID="txtReason" runat="server"></asp:Label>
- </td>
- </tr>
- <tr>
- <td align="right">
- 审批意见:
- </td>
- <td align="left">
- <asp:TextBox ID="txtManager" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td align="right">
- <asp:DropDownList ID="dopAction" runat="server">
- </asp:DropDownList>
- </td>
- <td>
- <asp:Button ID="btnSave" runat="server" Text="提交" OnClick="btnSave_Click" />
- </td>
- </tr>
- </table>
后台代码:
- protected void Page_Load(object sender, EventArgs e)
- {
- K2Util k2 = new K2Util();
- Connection k2con = k2.GetK2Connection();
- //根据SN号获取流程
- string sn = Request.QueryString["SN"];
- WorklistItem wi = k2con.OpenWorklistItem(sn);
- ProcessInstance pi = wi.ProcessInstance;
- //获取流程数据
- txtOriginator.Text = pi.DataFields["请假人"].Value.ToString();
- txtDays.Text = pi.DataFields["请假天数"].Value.ToString();
- txtReason.Text = pi.DataFields["请假原因"].Value.ToString();
- //加载活动Action列表
- dopAction.DataSource = wi.Actions;
- dopAction.DataTextField = "Name";
- dopAction.DataBind();
- k2con.Close();
- }
- protected void btnSave_Click(object sender, EventArgs e)
- {
- K2Util k2 = new K2Util();
- Connection k2con = k2.GetK2Connection();
- //根据SN获取流程
- string sn = Request.QueryString["SN"];
- WorklistItem wi = k2con.OpenWorklistItem(sn);
- ProcessInstance pi = wi.ProcessInstance;
- //设置审批意见
- pi.DataFields["经理审批意见"].Value = txtManager.Text.Trim();
- //执行Action操作
- wi.Actions[dopAction.Text].Execute();
- //提交按钮灰掉
- btnSave.Enabled = false;
- k2con.Close();
- }
4.流程列表界面 index.aspx
用户进入的向导页面,只有一些html代码。
- <body>
- <form id="form1" runat="server">
- <h3>
- K2 flow list:</h3>
- <div style="margin-left: 100px">
- <a href="DemoProcess/start.aspx" target="_blank">DemoProcess</a>
- <br />
- <a href="LeaveProcess/Startflow.aspx" target="_blank">LeaveProcess</a>
- </div>
- <div>
- <h3>
- <a href="MyWorklist.aspx" target="_blank">MyWorklist</a></h3>
- </div>
- </form>
- </body>
5.我的待办页面 MyWorklist.aspx
此页面展示当前登录用户的待办列表,前台是一个repeater,单击流程名称进入审批页面,前台代码如下:
- <table style=" 95%">
- <tr>
- <th>
- Folio
- </th>
- <th>
- SerialNumber
- </th>
- <th>
- ProcessName
- </th>
- <th>
- ActivityName
- </th>
- <th>
- EventName
- </th>
- <th>
- StartDate
- </th>
- </tr>
- <asp:Repeater ID="Mylist" runat="server">
- <ItemTemplate>
- <tr>
- <td>
- <%#DataBinder.Eval(Container.DataItem, "Folio").ToString()%>
- </td>
- <td>
- <%#DataBinder.Eval(Container.DataItem, "SerialNumber").ToString()%>
- </td>
- <td>
- <%#DataBinder.Eval(Container.DataItem, "ProcessName").ToString()%>
- </td>
- <td>
- <%#DataBinder.Eval(Container.DataItem, "ActivityName").ToString()%>
- </td>
- <td>
- <%#DataBinder.Eval(Container.DataItem, "EventName").ToString()%>
- </td>
- <td>
- <%#DataBinder.Eval(Container.DataItem, "StartDate", "{0:yyyy-MM-dd HH:mm}")%>
- </td>
- </tr>
- </ItemTemplate>
- </asp:Repeater>
- </table>
后台加载数据的代码:
- protected void Page_Load(object sender, EventArgs e)
- {
- Connection k2con = new Connection();
- // Connect to Workflow Server
- k2con.Open("SZ-JIANGWX:5252");
- //获取当前登录用户的待办列表
- Worklist wl = k2con.OpenWorklist();
- //create a data table
- DataTable dt = new DataTable();
- //add some columns
- dt.Columns.Add("Folio");
- dt.Columns.Add("SerialNumber");
- dt.Columns.Add("ProcessName");
- dt.Columns.Add("ActivityName");
- dt.Columns.Add("EventName");
- dt.Columns.Add("StartDate");
- foreach (WorklistItem wli in wl)
- {
- //create a new row, with columns defined
- DataRow dr = dt.NewRow();
- //add values from worklist + sub-objects
- dr["SerialNumber"] = wli.SerialNumber;
- dr["ProcessName"] = "<a href=" + wli.Data.ToString() + " target='_blank'>" + wli.ProcessInstance.FullName + "</a>";
- dr["ActivityName"] = wli.ActivityInstanceDestination.Name;
- dr["EventName"] = wli.EventInstance.Name;
- dr["StartDate"] = wli.EventInstance.StartDate.ToString();
- dr["Folio"] = wli.ProcessInstance.Folio;
- //add the data row to the data table
- dt.Rows.Add(dr);
- }
- //bind data table to grid
- Mylist.DataSource = dt;
- Mylist.DataBind();
- k2con.Close();
- }
6.运行流程:index进入startflow页面,startflow填写数据发起流程,index页面进入myworklist页面,myworklist页面单击待办列表进入审批页面。
7.这些api都是最常用的几个api,时间所限只能简单写成这样,最起码最简单的一个k2流程可以跑起来了,哈哈,下面推荐一些k2的学习链接和书。
电子书:K2_Blackpearl_产品白皮书.pdf k2产品的基础介绍
Wrox.Professional.K2.blackpearl.Feb.2009.eBook-DDU.pdf wrox出版的k2教程书,经典书,详细介绍了k2的开发知识,我正在看此书。
学习链接:http://www.cnblogs.com/kaixuanpisces/category/149223.html
http://www.cnblogs.com/hqbird/archive/2010/06/23/1763665.html
http://www.cnblogs.com/dannyli/