The latest version 0807 of K2 now has the task proxy function, which is called out-of-office. However, previous versions of K2 are not provided, in our projects, we usually have this requirement. Here I will not inherit the traditional habitsCodeInstead, I would like to explain the idea of implementing this function and the core code, which can be easily written by white people .. Actually, I am not a good guy either ..). The core code is as follows:
Code
1 This . Deleteoutdatedelegation ();
2 Datatable dt = This . Getdelegationlist ();
3 Datarowcollection rows = DT. Rows;
4 If (Rows. Count < 1 )
5 {
6Return;
7}
8 Using (Connection Conn = This . Openk2clientconnection ())
9 {
10 Foreach (Datarow row In Rows)
11 {
12 String From = Row [ " From " ]. Tostring ();
13 String To = Row [ " To " ]. Tostring ();
14 If ( String . Isnullorempty (from) | String . Isnullorempty ())
15 {
16Continue;
17}
18 Using (Impersonation. Impersonate (from, Conn ))
19 {
20 Worklist items = Conn. openworklist ();
21 Foreach (Worklistitem item In Items)
22 {
23 Item. Redirect ();
24 Helper. Sendmail (to, k2_taskurl );
25 // Log. Write (logtype. info, " Task " + Item. processinstance. Folio + " From " + From + " Redirection " + To + " . " );
26 System. Threading. thread. Sleep ( 5000 );
27 }
28 }
29 }
30 }
1) First, use the deleteoutdatedelegation method to clear expired proxy information (select required field from table where expired ).
2) use the getdelegationlist method to obtain valid proxy information (the field from table where required by the SELECT statement is valid ).
3) cyclically determine the agent and the agent, simulate the agent to forward the agent's tasks to the agent, and then send an email to the agent.
We know that workflowmanagementserver has a job transfer method redirectworklistitem (destination, "K2:" + to, procinstid, actinstdestid, Itemid ), in the beginning, we used this method to achieve the redirection. However, during project implementation, we found that this method is sometimes effective but sometimes ineffective, later, I used the simulated user and the worklistitem redirect of the client to implement this function.
The simulated class is also very simple. The code is here:
1 Public Class Impersonation: idisposable
2 {
3 Private Connection _ Conn = Null ;
4 Private Impersonation (connection conn)
5 {
6_ Conn=Conn;
7}
8 Public Static Impersonation impersonate ( String Username, connection conn)
9 {
10Conn. impersonateuser (username );
11Return NewImpersonation (conn );
12}
13 Public Void Dispose ()
14 {
15_ Conn. revertuser ();
16}
17 }
Getdelegationlist and deleteoutdatedelegation are the execution of stored data. For example, for SQL statements, our projects are generally Moss + K2, so our data is generally stored in the moss list.
That's simple. Of course, some things have different requirements. For example, some customers only need agents, and some customers also need to process agents for time periods, which can be easily implemented, SQL statement.
Put the code in an automatically executedProgramIs OK, such as Windows Services, of course, like the moss project can implement it in the scheduled task of moss.