Start a Send SmartPost process
Use this code example to create a small console application, which by the use of a client, prepares and invokes a SmartPost process.
The values of the code example are hardcoded. Before you use the example code, you must replace the values and the arguments with the parameters of the actual system and you must consider the requirements that you have for the Send SmartPost process.
For more information about the arguments and values used in the example, see SmartPost API.
Code to Start a Send SmartPost process
using System;
using System.Net;
using Scanjour.Workflow4.Web.Services.Entities;
using Scanjour.Workflow4.Web.Services.Test.Integration.Clients;
namespace SendSmartPostExample
{
/// <summary>
/// This class contains an example of how the ”Send SmartPost” can be invoked from a client.
/// </summary>
public class Program
{
/// <summary>
/// Template to form the URL to the Process Service Client.
/// </summary>
private const string ProcessTemplate = "http://{0}/Process/Process.svc/";
/// <summary>
/// The GUID, which uniquely identifies the “Send SmartPost” process definition.
/// </summary>
private const string SendSmartPostProcessGuid = "0577b569-f10b-47ea-8718-c8912ce70a0a";
/// <summary>
/// The name of the data source. Typically, this is the host name or IP-address of the server on which WZP is installed. Usually, you will
/// keep the name db01 and then setup the real IP-address of the server in your hosts-file.
/// </summary>
private const string Dsn = "db01";
/// <summary>
/// The username by which the program authenticates on the server hosting WZP.
/// </summary>
private const string Username = "Testadmin";
/// <summary>
/// The password by which the program authenticates on the server hosting WZP.
/// </summary>
private const string Password = "TA";
/// <summary>
/// The domain in which the username is registered.
/// </summary>
private const string Domain = "lmdom";
/// <summary>
/// The key of the case file from which the SmartPost process is started. The key is not the same as the file number. Please look in the
/// FILE-table where the file key (FILE_KEY) can be mapped from the file number (FILE_NO).
/// </summary>
private const string FileId = "81";
/// <summary>
/// The entry point to this example.
/// </summary>
public static void Main()
{
// Form the process URL.
var processUrl = new Uri(string.Format(ProcessTemplate, Dsn));
// Initiate the cridentials and create an instance of the Process Service Client.
var credentials = new NetworkCredential(Username, Password, Domain);
var client = new ProcessServiceClient(processUrl, credentials);
// Retrieve the definition of the process arguments (input parameters) from the definition on the server.
var startupInfo = client.GetProcessStartupInfo(SendSmartPostProcessGuid);
// The definition is a set of named properties, where each property has a name, a value and a type. Into the definition, we fill the
// values of the specific type.
SetProperty(startupInfo, "Subject", "This is the title of my shipment");
SetProperty(startupInfo, "ContextId", FileId);
SetProperty(startupInfo, "LetterRecordId", "10");
SetProperty(startupInfo, "AttachmentRecordIds", new[] {"368", "387"});
SetProperty(startupInfo, "PartIds", new[] {"178"});
SetProperty(startupInfo, "CopyPartIds", new[] { "179", "139" });
SetProperty(startupInfo, "IsEnableReply", false);
SetProperty(startupInfo, "Deadline", new DateTime(2016, 10, 20));
SetProperty(startupInfo, "IsPreview", true);
SetProperty(startupInfo, "IsApproval", false);
SetProperty(startupInfo, "LogicalSubscriptionGroupId", "1001");
SetProperty(startupInfo, "ShipmentTypeID", "1");
SetProperty(startupInfo, "IsDeleteOriginal", false);
SetProperty(startupInfo, "RemotePrintTypeId", "0");
SetProperty(startupInfo, "DecisionCode", "");
SetProperty(startupInfo, "DecisionCodeDissension", "");
SetProperty(startupInfo, "DecisionDate", DateTime.Now);
SetProperty(startupInfo, "CaseState", null, typeof(string));
SetProperty(startupInfo, "CloseCase", false);
SetProperty(startupInfo, "LetterTypeId", "0");
SetProperty(startupInfo, "AdditionalCases", new string[] {});
// Ask the server to invoke the process with the defined properties.
var instance = client.StartProcess(startupInfo, "File", FileId);
}
/// <summary>
/// Sets a single property in the startup information.
/// </summary>
/// <param name="startupInfo">The startup information in which a single property is set.</param>
/// <param name="name">The name of the property.</param>
/// <param name="value">The value to which the property is set.</param>
/// <param name="type">The type of the property. If null, then the type is derived from the specified value.</param>
private static void SetProperty(ProcessStartupInfo startupInfo, string name, object value, Type type = null)
{
if (value == null && type == null)
throw new InvalidOperationException("Either value or type cannot be null.");
startupInfo.Properties[name] = new Property
{
Name = name,
Value = value,
Type = DetermineType(name, value, type),
};
}
/// <summary>
/// Determines the type of a property from it’s value or alternatively (if the value is null) from the specified type.
/// </summary>
/// <param name="name">The name of the property, which type will be determined.</param>
/// <param name="value">The value from which the type is being derived.</param>
/// <param name="type">The alternate type. (Used if value is null).</param>
/// <returns>The determined type.</returns>
private static Type DetermineType(string name, object value, Type type)
{
if (type != null)
return type;
var valueType = value.GetType();
if(valueType == null)
throw new InvalidOperationException($"Cannot determine type of property ('{name}').");
return valueType;
}
}
}