Imports System.IO
Imports System.Web.Configuration
Imports Laserfiche.Workflow
Imports Laserfiche.Workflow.Objects
Imports Laserfiche.Workflow.Objects.Instances
'Browse to .NET 4.0 MSIL GAC, And navigate to the Laserfiche.Workflow.ServerObjects.dll assembly.
'Path: c : \windows\Microsoft.Net\assembly\GAC_MSIL\Laserfiche.Workflow.ServerObjects\v4.0_8.3.0.0__d8428ff9263e6cda\Laserfiche.Workflow.ServerObjects.dll
' NOTE: You can use the "Rename" command on the context menu to change the class name "Service" in code, svc and config file together.
Public Class Service
Implements IService
Public g As Guid
Public Sub New()
End Sub
Public Function LFTriggerNewDocument(ByVal SolutionName As String(),
ByVal ProcessName As String(),
ByVal TemplateName As String(),
ByVal FieldNames As String(),
ByVal FieldValues As String(),
ByVal Base64 As String()
) As Result Implements IService.LFTriggerNewDocument
Dim result As New Result With {
.Succeeded = False,
.ErrorMessage = ""
}
Try
g = Guid.NewGuid()
trace("Process Started at: " & Now.ToString("yyyy-MM-dd hh:mm:ss:tt"))
trace("Instance ID: " & g.ToString)
Dim SolutionNameParam As New InstanceParameter()
SolutionNameParam.SetValue(SolutionName)
SolutionNameParam.Name = "SolutionName"
Dim ProcessNameParam As New InstanceParameter()
ProcessNameParam.SetValue(ProcessName)
SolutionNameParam.Name = "ProcessName"
Dim TemplateNameParam As New InstanceParameter()
TemplateNameParam.SetValue(TemplateName)
SolutionNameParam.Name = "TemplateName"
Dim FieldNamesParam As New InstanceParameter()
FieldNamesParam.SetValue(FieldNames)
SolutionNameParam.Name = "FieldNames"
Dim FieldValuesParam As New InstanceParameter()
FieldValuesParam.SetValue(FieldValues)
SolutionNameParam.Name = "FieldValues"
Dim Base64Param As New InstanceParameter()
Base64Param.SetValue(Base64)
SolutionNameParam.Name = "Base64"
Dim workflowName As String = WebConfigurationManager.AppSettings("LFTriggerNewDocumentWorkflowName")
Dim WfinputParams As New List(Of InstanceParameter)
WfinputParams.Add(SolutionNameParam)
WfinputParams.Add(ProcessNameParam)
WfinputParams.Add(TemplateNameParam)
WfinputParams.Add(FieldNamesParam)
WfinputParams.Add(SolutionNameParam)
WfinputParams.Add(Base64Param)
'Dim WfinputParams As New Dictionary(Of String, InstanceParameter) From {
' {"SolutionName", SolutionNameParam},
' {"ProcessName", ProcessNameParam},
' {"TemplateName", TemplateNameParam},
' {"FieldNames", FieldNamesParam},
' {"FieldValues", FieldValuesParam},
' {"Base64", Base64Param}
'}
'result = TriggerWorkflow(workflowName, g, WfinputParams, True)
'Output Parameter example
Dim WfOutputParams As New Dictionary(Of String, String) From {
{"Succeeded", Nothing},
{"ErrorMessage", Nothing}
}
result = TriggerWorkflow(workflowName, g, WfinputParams, True, WfOutputParams)
Catch ex As Exception
result.Succeeded = False
result.ErrorMessage = ex.Message
Log(result.ErrorMessage)
trace(result.ErrorMessage)
End Try
Return result
End Function
'''
'''
'''
'''
''' this ID will be the workflow instanceID and the workflow RuleID,
''' you can use the RuleId to Search for the workflow in the workflow designer
''' provide the list of workflow input parameters as Dictionary(Of String, String)
''' set this flag to true if you need to get the reuslt of the workflow
''' or you need to check if the workflow is completed successfully
''' if you need to get the workflow output params,
''' provide the list of the needed output params as Dictionary(Of String, String)
''' where the key is the workflow name and the value s empty ex {"Param1",""}
'''
Function TriggerWorkflow(ByVal WorkflowName As String, ByVal InstanceID As Guid,
ByVal parameters As IEnumerable(Of InstanceParameter) _
, Optional ByVal WaitToFinish As Boolean = False _
, Optional ByRef OutpuParameters As Dictionary(Of String, String) = Nothing) As Result
Dim Result As New Result With {
.Succeeded = False
}
Dim workflowServer As String = WebConfigurationManager.AppSettings("WorkflowServer")
Dim workflowApplication As String = WebConfigurationManager.AppSettings("ApplicationName")
trace("triggeing workflow: " & WorkflowName)
trace("Workflow Server: " & workflowServer)
trace("Application Name: " & workflowApplication)
'trace("Workflow Name : " + WorkflowName)
trace("Input parameters count : " + parameters.ToArray.Count.ToString)
Using app As WorkflowApplication = New WorkflowApplication With {
.Registration = New WorkflowServerRegistration(workflowServer),
.ApplicationName = workflowApplication,
.ApplicationType = WorkflowApplicationType.Client
}
' The initiator of the workflow, it can be null.
Dim initiator As Initiator = Nothing
' Options for creating the workflow, this can be null.
' The starting rule for this workflow (it can be made up).
Dim ruleName As String = InstanceID.ToString
' Optional parameters to the workflow, they are available as tokens.
Using connection As WorkflowConnection = app.Open()
trace("Connected to the Workflow Server Successfully")
Dim Creationoptions As WorkflowCreationOptions = New WorkflowCreationOptions With {
.WorkflowInstanceId = InstanceID
}
Dim workflow As PublishedWorkflow = connection.Database.GetPublishedWorkflow(WorkflowName)
workflow.StartWorkflowDirectly(ruleName, Nothing, initiator, Creationoptions, parameters)
trace("Workflow Started Successfully")
If WaitToFinish Then
trace("Waiting for the workflow to complete")
Dim SearchOptions As SearchFilterOptions = New SearchFilterOptions() With {
.InstanceId = InstanceID,
.WorkflowRule = InstanceID.ToString
}
Dim maxLoop As Integer = WebConfigurationManager.AppSettings("MaxLoopCount")
Dim counter As Integer = 0
Dim instance As WorkflowInstance
For Each searchresult As SearchResult In connection.Database.Tracking.SearchWorkflowInstances(SearchOptions)
instance = searchresult.WorkflowInstance
instance.Refresh()
While (instance.Status <> WorkflowInstanceStatus.Completed And instance.Status <> WorkflowInstanceStatus.Terminated)
Threading.Thread.Sleep(WebConfigurationManager.AppSettings("SleepDuration"))
counter += 1
If counter >= maxLoop Then
Exit While
End If
instance.Refresh()
End While
Select Case instance.Status
Case WorkflowInstanceStatus.Completed
Result.Succeeded = True
If OutpuParameters IsNot Nothing Then
Dim keys = OutpuParameters.Keys
For Each param In instance.GetOutputParameters.Where(Function(p) keys.ToArray.Contains(p.Name))
OutpuParameters(param.Name) = param.Value(0).ToString
Next
End If
trace("Workflow Completed Successfully")
Case WorkflowInstanceStatus.Terminated
Result.Succeeded = False
Dim errorMsg As String = ""
For Each param In instance.GetOutputParameters.Where(Function(p) p.Name = WebConfigurationManager.AppSettings("errorMessageParam"))
errorMsg = param.Value(0).ToString
Next
Result.ErrorMessage = "Error while Executing the workflow: " & errorMsg
trace(Result.ErrorMessage)
Log(Result.ErrorMessage)
Case Else
Result.Succeeded = False
Result.ErrorMessage = "Operation Time Out"
trace(Result.ErrorMessage)
Log(Result.ErrorMessage)
End Select
Next
If Result.Succeeded = False And Result.ErrorMessage = "" Then
Result.Succeeded = False
Result.ErrorMessage = "Error Wile Searching for Workflow Instance, Please check if the instance is triggered properly"
trace(Result.ErrorMessage)
Log(Result.ErrorMessage)
End If
Else
Result.Succeeded = True
End If
End Using
End Using
Return Result
End Function
Public Sub trace(ByVal str As String)
If WebConfigurationManager.AppSettings("TraceFlag") = 1 Then
Using writer As StreamWriter = New StreamWriter(WebConfigurationManager.AppSettings("tracePath") & "\" & Now.ToString("yyyy-MM-dd") & "-" & g.ToString & ".txt", True)
writer.WriteLine(Now.ToString("yyyy/MM/dd - hh:mm ss") & ": " & str)
End Using
End If
End Sub
Public Sub Log(ByVal str As String)
Using writer As StreamWriter = New StreamWriter(WebConfigurationManager.AppSettings("LogPath"), True)
writer.WriteLine(Now.ToString("yyyy/MM/dd - hh:mm ss") & ": " & g.ToString & " " & str)
End Using
End Sub
End Class