Search

Leave Management sample with source code

Minimize

This sample illustrates how a Windows Forms application can integrate with the cDevWorkflow product.  This forms application demonstrates the following:

  • A leave request is entered by a user, request is created and a workflow instance is initiated
  • A user is able to view a list their assigned tasks
  • User is able to render a task and approve or reject the leave request from the task
  • Based on the user's actions, workflow instance is able to update the status of the leave request (approved/rejected)

This sample also demonstates few technology differences.  Windows Forms application is written using .Net Version 3.5 and since .Net 3.5 cannot make calls to the cDevWorkflow .Net 4.0 API, this sample demonstrates how to use the cDevWorkflow Webservice API.  This is a common issue with older applications, but cDevWorkflow provides a powerful Webservice API that is strong as the .Net 4.0 API.

Installation files including the source code are available here, download the "leaverequestapplication.zip" file and unpack the file to C:\LeaveRequestApplication

Note: In order to run this sample you will require cDevWorkflow Version 3.8.

Note: Also add the following connection string

<add name="lmConnectString" connectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=LeaveManagementTracking;Data Source=(local)" providerName="System.Data.OLEDB"/>

to the following two configuration files:

  • C:\inetpub\wwwroot\cDevWorkflow\web.config
  • C:\inetpub\wwwroot\cDevWorkflow\bin\cDevWorkflowService.exe.config


Step 1:  Database

  • Open SQL Server Manager and created a database called "LeaveManagementTracking"
  • Navigate to the following directory C:\LeaveRequestApplication\DBScript and execute the SQL file called "dbTableScript.sql" to create the database table.

Step 2:  Importing the Workflow Definitions

  • Open cDevWorkflow Configuration Manager
  • Navigate to the "Definitions" tab
  • Create a Definition called "BBI Leave Request Def"

  • Once the Workflow definition "BBI Leave Request Def" is created, select the definition from the list and click the "Browse" button to locate the definition file.  Navigate to the directory C:\LeaveRequestApplication\WorkflowDefinition and select the called "BBI Leave Request Def.xml" and clikc the "Update Definition" button to import the definition.

  • Once the definition is updated, select the definition and click the "Render Definition" button.  If the definition was updated, rendered definition should look as follows:

Step 3: Executing the application

  • Navigate to the application directory C:\LeaveRequestApplication and open the solution file LeaveRequestApplication.sln using MS Visual Studio 2010.
  • Execute the application.

 
Step 4: Creating a Leave Request

  • Once the application starts, the main screen lets you create a new Leave request, enter the necessary information for the Leave request and click the "Create Leave Request" button to create it.

  • After creating the leave request, you should see the following confirmation dialog:

  • Newly created Leave request is rendered as follows:

  • Click the "My Tasks" tab to view the tasks assigned to the user.  Click the task to render the information about he Leave request:

       
  • Click the "Approve Leave Request" button to approve the request.

  • After approving the request, click the "Request Leave" tab.  Now the leave request should display its status as "Approved".

Code Explanations:

The following code sample create the  runtime service object and sets the user context for the web service:

//create the runtime object using the reference to the webservice
oRuntime = new cDevRuntimeService.RuntimeServiceClient();
 
//set the user for the runtime object
oRuntime.setUser(clsConfig.workflowUser);

The following function demonstrates how to instantiate a workflow instance and pass the leave request id as an input parameter.

private void submitToWorkflow(string sLeaveRequestID)
{
   string instanceID = "";
 
   //create an instance object using the workflow definition
   bool bFlag = oRuntime.createInstance(clsConfig.workflowDefinition, Guid.NewGuid().ToString(), ref instanceID);
 
   if (bFlag)
   {
      //prepare the hashtable xml for the input parameters to the instance execution
      string sInputParms = string.Format("<hashtable><item key=\"leaveReqID\" value=\"{0}\"/></hashtable>", sLeaveRequestID);
 
      //execute the workflow instance using the input parameters
      bFlag = oRuntime.execute(instanceID, sInputParms);
   }
}

The following function gets a list of tasks for the current user using the web service and renders the list.

private void populateTasks()
{
   //get a list of open tasks for the current user
   string sTaskXML = oRuntime.getOpenTasks();
 
   //load the task xml into a data set
   DataSet oDS = new DataSet();
   oDS.ReadXml(new StringReader(sTaskXML));
 
   gdvTasks.AutoGenerateColumns = false;
 
   //bind the tasks to the grid
   gdvTasks.DataSource = oDS.Tables[0].DefaultView;
 
   gdvTasks.Columns["taskID"].Visible = false;
   gdvTasks.Columns["routedItem"].Visible = false;
 
   gdvTasks.ClearSelection();
   gdvTasks.Refresh();
 
}

The following function lets the user approve the leave request.

private void btnApprove_Click(object sender, EventArgs e)
{
   //get the task ID for the selected task
   string sTaskID = gdvTasks.Rows[gdvTasks.CurrentRow.Index].Cells["taskID"].Value.ToString();
 
   //make a web service call to approve the task
   oRuntime.completeTask(sTaskID, "completed", "");
 
   //udpate the task list
   populateTasks();
}

The following code fragment lets the user reject the leave request

private void btnReject_Click(object sender, EventArgs e)
{
   //get the task ID for the selected task
   string sTaskID = gdvTasks.Rows[gdvTasks.CurrentRow.Index].Cells["taskID"].Value.ToString();
 
   //make a web service call to reject the task
   oRuntime.completeTask(sTaskID, "rejected", "");
 
   //update the task list
   populateTasks();
}