|
private void ReassignAndNotify(string strK2ServerName, // name of K2 Server
string strSmtpServer, // name of SMTP Server
string strNewUserID, // fully qualified user ID to assign task too (Domain\UserName)
string strNewUserEmail, // email address to send the reassignment notification to
string strDestination, // fully qualified user ID (Domain\UserName) of the person who is assigned a K2 task
string strProcessName, // desired K2 process name (optional)
string strActivityName, // desired K2 activity name (optional)
string strEventName, // desired K2 event name (optional)
string strFolio, // desired K2 folio name (optional)
string strFromDate, // desired K2 task start date (optional)
string strToDate) // desired K2 task end date (optional)
{
/*
* This method will reassign a worklist item(s) from one person to another and notify the new
* user via email that a task has been reassigned to them. This notification email will also contain
* a link to allow them to action the item.
*
* Please note, K2MNG permits programatic functionality for an administrator to reassign (called Redirect)
* a task from one user to another. However it does not have any ability to automatically generate
* email notifications. Also, the K2MNG worklistitem object does not expose the specific data required
* to action an item (i.e. Serial Number of Data field). As such, this method implements the following
* logic to acheived the desired functionality:
*
* 1. Retrieve a group of worklist items via K2MNG (logged in as a K2Server admin)
* 2. Redirect the worklist items to the currently logged in user to K2Mng (not necessarily the desired final user)
* 3. Use K2ROM to open the newly reassigned worklist item under the credentials of the currently
* logged in user
* 4. Retrieve the serial number and data (which contains the URL to the task) fields into local variables
* 5. Use the K2ROM WorklistItem.Redirect method to reassign task to true new user
* 6. Generate an email with the .NET class to notify new user of task assignment. The email body
* includes the link to the task that was recovered from the K2ROM worklistitem
* 7. Steps 2-6 occur for each K2MNG worklist item recovered in step 1
* 8. Close the K2MNG and K2ROM connections
*
* When a task is redirected/reassigned by any K2 API there a new task is not physically created within
* the K2 system; what occurs is simply the user assigned to that task is changed. This permits us
* to recover the task URL (in the Data field) when it is temporarily assigned to the K2Server admin.
*
* VS.NET project references need to be made to the following files:
* - C:\Program Files\K2.net 2003\Bin\K2Mng.dll
* - C:\Program Files\K2.net 2003\Bin\K2ROM.dll
*/
SourceCode.K2Mng.K2Manager oK2Mng = new SourceCode.K2Mng.K2Manager();
SourceCode.K2ROM.Connection oConn = new SourceCode.K2ROM.Connection();
// 5252 is the default K2 port
int nK2Port = 5252;
// connection string containing the credentials of a K2 server admin to be used by k2 connections. THIS SHOULD BE CHANGED FOR YOUR PROJECT.
string strAdminConnectString = "K2Domain,Administrator,k2";
// open a K2ROM connection as the k2 server admin
oConn.Open(strK2ServerName, strAdminConnectString);
// open a K2Mng Connection
// ***NOTE *** - In order for the
// redirect functionality to work, a K2MNG connection needs to be made with a K2 Server admin
// credentials (as setup in K2 Service Manager). Since this is being programatically handled and
// the functionality exposed via an application, this can be coded via a connection string as follows:
// oK2Mng.Login(K2ServerName, K2PortNumber, "ADDomain,UserName,Password");
//
oK2Mng.Login(strK2ServerName, nK2Port, strAdminConnectString);
// retrieve a list of worklist items
SourceCode.K2Mng.WorkListItems oWorklist = oK2Mng.GetWorkListItems(strDestination,
strProcessName,
strActivityName,
strEventName,
strFolio,
strFromDate,
strToDate);
// iterate the list of service manage worklist items
foreach (SourceCode.K2Mng.WorkListItem oItem in oWorklist)
{
// temporarily reassign to the current logged in K2ROM user. This permits the Serial Number to be retrieved from the Data
// Data property and thus can be reused in the email
oK2Mng.RedirectWorklistItem(Convert.ToInt32(oItem.ID), oConn.User.FQN);
// now use K2ROM to return a filtered worklist. the filter will be the
// full process name and the event name
SourceCode.K2ROM.WorklistCriteria oCrit = new SourceCode.K2ROM.WorklistCriteria();
oCrit.AddFilterField(SourceCode.K2ROM.WCField.ProcessFullName, SourceCode.K2ROM.WCCompare.Equal, oItem.ProcName);
oCrit.AddFilterField(SourceCode.K2ROM.WCField.EventName, SourceCode.K2ROM.WCCompare.Equal, oItem.EventName);
// retrieve the filtered list
SourceCode.K2ROM.Worklist oWL = oConn.OpenWorklist(oCrit);
if (oWL.Count > 0)
{
// iterate the returned items, in case there are more than one for this process name / event name combination
foreach (SourceCode.K2ROM.WorklistItem oWli in oWL)
{
// find the same worklist item that was opened above via K2Mng
if (oWli.ID == oItem.ID)
{
// retrieve the details of the real/K2ROM worklist item
string strSerialNumber = oWli.SerialNumber;
string strData = oWli.Data; // this usually contains a URL
// reassign this task to the real desired user now that we have the runtime task details (Serial Number and Data)
oWli.Redirect(strNewUserID);
// generate an email via .NET code here to the new user
string strFrom = "admin@k2server.com";
string strEmail = strNewUserEmail;
string strSubject = "You have been reassigned a workflow task";
string strBody = "";
System.Web.Mail.MailMessage objMsg = new System.Web.Mail.MailMessage();
System.Web.Mail.SmtpMail.SmtpServer = strSmtpServer;
if ((strEmail == null) || (strEmail.ToString().Trim().Length == 0))
{
throw new System.Exception("No Email Address was supplied, please rectify and try again.");
}
strBody = "Please click link below to action the item."
+ System.Environment.NewLine
+ "" + System.Environment.NewLine
+ strData;
objMsg.From = strFrom.ToString();
objMsg.Subject = strSubject.ToString();
objMsg.Body = strBody.ToString();
objMsg.To = strEmail.ToString();
objMsg.Subject = strSubject.ToString();
objMsg.Body = strBody.ToString();
// send the message
System.Web.Mail.SmtpMail.Send(objMsg);
break; // end looping through K2ROM worklist items for this specific process/event combo
}
}
}
}
// close the K2API connections
oK2Mng.Logout();
oConn.Close();
} |