Extend Workflow Functionality with the Workflow SDK « Solution Exchange Solution Exchange

Solution Exchange

Extend Workflow Functionality with the Workflow SDK

Carl Sykes, Software Engineer at Laserfiche, describes three innovative ways for utilizing the Laserfiche Workflow SDK.

Contributed by:Carl Sykes, Software Engineer, Laserfiche

Laserfiche Workflow SDK comes standard with Laserfiche Workflow 8.3 (though you have to install it separately). Like the Laserfiche SDK, which allows you to retrieve or send information to the Laserfiche Server, the Workflow SDK allows you to retrieve or send information to the Workflow Server. You can use it to do everything that the Workflow Server can do, including:

  • Start/terminate workflows.
  • Get the status of specific workflows.
  • Export/import workflows.
  • Build custom Workflow activities.

Here are three different C# projects that use the Workflow SDK to:

  1. Initiate a workflow for a particular entry directly from within the Laserfiche Client.
  2. Retrieve workflow status and terminate a workflow for a particular entry directly from within the Laserfiche Client.
  3. Trigger a workflow using data entered into a form outside of Laserfiche.

Note: Before using the code samples below, you may want to familiarize yourself with Workflow SDK basics in this whitepaper: Getting Started with the Workflow SDK (Visual C#). A Support Site account is required to access this white paper.

Example 1: Initiate a workflow for a particular entry directly from within the Laserfiche Client

Scenario: Your organization has scheduled a particular workflow to run every evening to process invoices from a particular folder in Laserfiche. Every once in a while, an invoice will need to be processed and approved right away. You can add a button to your Laserfiche Client that, when clicked, will enable you to select a particular workflow from a list, and initiate it directly.

Here is how this process works:

  • Select the document in Laserfiche that you would like to process with Workflow.
  • Click on the custom button in the toolbar to launch a form that lists all available workflows.
  • Select the workflow that you would like to run and click Start.
  • This workflow will then be initiated and the invoice processed.

Here is the code for generating a list of available workflows.

Code Section

private void FillWorkflows()
{
WorkflowApplication application = new WorkflowApplication();
application.Registration = new WorkflowServerRegistration("localhost");
using (WorkflowConnection conn = application.Open())
{
foreach (PublishedWorkflow workflow in conn.Database.GetAllPublishedWorkflows())
{
ListViewItem item = new ListViewItem(workflow.Name);
this.listView1.Items.Add(item);
}
}
}

Here is the code for the Start button.

Code Section

private void StartWorkflow(string workflowName, string ruleName, StartingEntry entry, Initiator initiator)
{
WorkflowApplication application = new WorkflowApplication();
application.Registration = new WorkflowServerRegistration("localhost");
using (WorkflowConnection conn = application.Open())
{
PublishedWorkflow workflow = conn.Database.GetPublishedWorkflow(workflowName);
workflow.StartWorkflow(ruleName, entry, initiator, null, null);
}
}

Example 2: Retrieve workflow status and terminate a workflow for a particular entry directly from within the Laserfiche Client


Scenario: You need to delete an invoice but you want to make sure it isn’t currently being used by a workflow. If you do notice that it is being used by a workflow, terminate that workflow instance before deleting the invoice. To do this, you can add a custom button to your Laserfiche Client which will display a list of all workflows currently processing this document and enable you to terminate each instance before deleting the invoice.

Here is how this process works:

  • Select the document in Laserfiche that you would like to delete.
  • Click on the custom button in the toolbar to launch a form that lists the status of all the workflows that are currently using this document.
  • Select each of the workflows that you would like to terminate and click on Terminate.
  • The workflows will then be terminated and you can delete the document.

Here is the code to list the workflows that are using the selected entry.

Code Section

private void FindInstances(int entryId, WorkflowConnection conn)
{
SearchFilterOptions options = new SearchFilterOptions();
options.MinEntryId = entryId;
options.MaxEntryId = entryId;
IEnumerable results = conn.Database.Tracking.SearchWorkflowInstances(options);
foreach (SearchResult result in results)
{
string workflowName = result.WorkflowInstance.WorkflowName;
string username = result.WorkflowInstance.Initiator.Name;
string startTime = result.WorkflowInstance.StartTime.ToString();
string statusTime = result.WorkflowInstance.StatusChanged.ToString();
Guid wfIntanceId = result.WorkflowInstance.InstanceId;
WorkflowInstanceStatus status = result.WorkflowInstance.Status;
bool isWaiting = status != WorkflowInstanceStatus.Completed && status != WorkflowInstanceStatus.Terminated;
AddItemToList(workflowName, username, startTime, statusTime, isWaiting, wfInstanceId);
}
}

Here is the code for the Terminate button.

Code Section

private void TerminateInstance(Guid workflowInstnaceId, WorkflowConnection conn)
{
WorkflowInstance instance = conn.Database.Tracking.RetrieveWorkflowInstance(workflowInstanceId);
if (instance != null)
{
instance.Terminate();
}
}

Example 3: Trigger a workflow using data entered into a form outside of Laserfiche


Scenario: Every time a new employee is hired at your organization, the human resources department needs to e-mail that employee forms to fill out. Once the new employee fills out these forms, they need to be stored in the employee’s folder in Laserfiche. To streamline this process, HR staff can fill out a simple form and click a button to initiate a workflow that will create a new folder in the repository for the new hire and e-mail that person the required forms.

Here is how this process works:

  • Create a shortcut on your desktop to the New Employee application.
  • Double-click on the shortcut to launch a form.
  • After filling out the First Name, Last Name and Email fields, click on Add Employee.
  • This initiates a workflow that will create a new folder in the repository for the new hire and e-mail him the required forms. The values that were entered in the fields are used as tokens in the workflow.

Here is the code for the Add Employee button.

Code Section

private void AddNewEmployee(string firstName, string lastName, string email, WorkflowConnection conn)
{
Dictionary parameters = new Dictionary();
PublishedWorkflow newEmployeeWF = conn.Database.GetPublishedWorkflow("BPM 252 – New Employee");
parameters.Add("FirstName", firstName);
parameters.Add("LastName", lastname);
parameters.Add ("EmployeeEmail", email);
newEmployeeWF.StartWorkflow(parameters);
}

Some of the products and services listed on the Laserfiche Solution Exchange were not developed by Laserfiche. The recommendations and opinions expressed on the Laserfiche Solution Exchange are those of the person or persons posting the recommendations only, and they do not necessarily represent Laserfiche's opinion or recommendation of the product or service being reviewed. Laserfiche disclaims all liability resulting from your purchase or use of any non-Laserfiche software product or service listed on the site.

  • Share:
0 Appreciate this Article

Comment (0) | Post your own

Print