Exploring SourceCode.Workflow.Management  GetWorkListItems method. (Worklist Criteria Filter)

Capture

This Post will explain you how to retrieve K2Worklist using SourceCode.Workflow.Management GetWorkListItems method and its Worklist Criteria Filter, using C# assembly.

We already know that K2 have provided us 2 assemblies as mentioned below, which we can use to retrieve Worklist items.

  • SourceCode.Workflow.Management
  • SourceCode.Workflow.Client.

But the difference between them is, using  SourceCode.Workflow.Management  you can get complete worklist irrespective of user whereas using SourceCode.Workflow.Client  you can’t as the latter gives the worklist of only a specific user under which the code is being run. i.e. Now if you are running the code under my credentials domain\Vijay then you will get the worklist of only this user domain\Vijay.

Now let’s see how we use this  Workflow.Management Assembly.

Step 1: Add a new Class library project using Visual Studio, give your project & class file a name and you need to add reference of below 2 assemblies to the project. You can find these assemblies in ~drive/K2Blackpearl/bin folder

using SourceCode.Hosting.Client.BaseAPI;
using SourceCode.Workflow.Management;

**Hosting.Client.BaseAPI is part of SourceCode.HostClientAPI assembly

Step 2: Create ConnectionBuilderString as below.

SCConnectionStringBuilder connectionString = 
                      new SCConnectionStringBuilder();
connectionString.Authenticate = true;
connectionString.Host = denallix;//Server Name Here
connectionString.Integrated = true;
connectionString.IsPrimaryLogin = true;
connectionString.Port = 5555;

Step 3: Create WorkflowManagementServer object and open the Connection

WorkflowManagementServer workflowServer = new WorkflowManagementServer();
workflowServer.CreateConnection();       
workflowServer.Connection.Open(Convert.ToString(connectionString));

Step 4: Now use workflowserver object to call get complete WorkList

WorklistItems K2WorkListItems = workflowServer.GetWorklistItems(
    string.Empty, string.Empty,string.Empty, string.Empty, string.Empty, 
    string.Empty, string.Empty);

Step 5: Don’t forget to close the connection

 workflowServer.Connection.Close();

That’s it. Pretty Simple and fast right. Yes it is!! Now just add a console project to same above solution and call this method to debug and test this method.

Now let’s explore the options available in workflowServer.GetWorklistItems.              K2 has provided total 6 overloaded methods as below.

Option 1:

public WorklistItems GetWorklistItems(WorklistCriteria wl);

Option 2:

public WorklistItems GetWorklistItems(WorklistCriteriaFilter filter);

Option 3:

public WorklistItems GetWorklistItems(string destination, 
           string processName, string activityName, string eventName, 
           string folio, string fromDate, string toDate);

Option 4:

public WorklistItems GetWorklistItems(DateTime fromDate, 
              DateTime toDate, string destination, string processName, 
              string activityName, string eventName, string folio);

Option 5:

public WorklistItems GetWorklistItems(DateTime fromDate, 
             DateTime toDate, string destination, string processName, 
             string activityName, string eventName, string folio, 
             int start, int count, out int recordCount);

 

Option 6:

public WorklistItems GetWorklistItems(List<DateTime> fromDate, 
             List<DateTime> toDate, List<string> destination, 
             List<string> destinationCoparison, 
             List<string> destinationCondition, 
             List<string> processName, List<string> processNameComparison, 
             List<string> processNameCondition, List<string> activityName, 
             List<string> activityNameComparison, 
             List<string> activityNameCondition, 
             List<string> eventName, List<string> eventNameComparison, 
             List<string> eventNameCondition, List<string> folio, 
             List<string> folioComparison, List<string> folioCondition, 
             string StartIndex, string PageSize);

 

Out of above, option 1, 5, 6 are going to be removed soon so we shall ignore them and see how we can use the remaining options available.

Using Option 2:

public WorklistItems GetWorklistItems(WorklistCriteriaFilter filter);

To use this option we need to pass an object of type WorklistCriterialFilter which is available in assembly SourceCode.Workflow.Management.Criteria, let’s see how we can create and use this filter

In namespaces section add

using WMC = SourceCode.Workflow.Management.Criteria;

Now in the method you can use conditions as below

  • Filter for Folio = 1234
WMC.WorklistCriteriaFilter WLC = new WMC.WorklistCriteriaFilter();
WLC.AddRegularFilter(WorklistFields.Folio, WMC.Comparison.Equals, "1234");
WorklistItems K2WorkListItems = workflowServer.GetWorklistItems(WLC);
  • Filter for items where Folio contains word approval
WLC.AddRegularFilter(WorklistFields.Folio, 
                     WMC.Comparison.like, "%approval%");
  • Filter for items where ProcessName = POC.WKF.StudentsAdmission
WLC.AddRegularFilter(WorklistFields.ProcessFullName, 
                   WMC.Comparison.Equals, "POC.WKF.StudentsAdmission");
*ProcessName should be FullName of process with path.
  • Filter for items where ProcessName = WKF.StudentsAdmission AND Folio contains word approval
WLC.AddRegularFilter(WorklistFields.ProcessFullName, 
                     WMC.Comparison.Equals, "POC.WKF.StudentsAdmission"); 

WLC.AddRegularFilter(WorklistFields.Folio, WMC.Comparison.Like, 
                     "%approval%", WMC.RegularFilter.FilterCondition.AND);

 

  • Filter for items where ProcessName = WKF.StudentsAdmission OR Folio contains word approval
WLC.AddRegularFilter(WorklistFields.ProcessFullName, WMC.Comparison.Equals,
                     "POC.WKF.StudentsAdmission");           

WLC.AddRegularFilter(WorklistFields.Folio, WMC.Comparison.Like, 
                     "%approval%", WMC.RegularFilter.FilterCondition.OR);

 

These are some of the various conditions that you can try with WorklistCriteriaFilter.

Using Option 3 & 4:

                Both of these options are self-explanatory. You just have to pass the respective parameters for applying those filters.

  • Complete Worklist
WorklistItems K2WorkListItems = 
             workflowServer.GetWorklistItems(string.Empty, string.Empty, 
             string.Empty, string.Empty, string.Empty, string.Empty, 
             string.Empty);

 

  • Worklist for a Process (POC.WKF.StudentsAdmission)
WorklistItems K2WorkListItems = 
               workflowServer.GetWorklistItems(string.Empty, 
              "POC.WKF.StudentsAdmission", string.Empty, string.Empty, 
              string.Empty, string.Empty, string.Empty);

 

Now you can try rest of the options 🙂

I’ll create another post explaining the API SourceCode.Workflow.Client.

Advertisements

Mixed mode assembly error fix while using K2 Assemblies

Helooo Every one

Among the articles that I have posted at the beginning of my blog UnCode K2, one of the article explains about File Load Exception which occurs when you are referencing a .Net Assembly built on Version 2, with in a project which is being built on Version 4 or above.

fileloadexception

More or less all source code assemblies which we get from K2 were built on .Net Framework Version 2 and when we use them in our version 4 projects we would encounter this issue.

As a work around we have to add some additional configuration settings in your app.config file to get around this issue.

<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

 

So basically while you run your application, above mentioned configuration inform .Net runtime to use mixed mode CLR 2 assemblies in a .NET 4 application so that they would load without any issue.

I thought above mentioned configuration will work in all cases, but actually it’s not.

Recently I was working on creating some Custom WCF Services which has dependencies on custom assemblies in which they have referredK2 source code assemblies. Now I’m able to build the project without any issue, but at the runtime I have encountered the above mentioned issue when I’m calling those methods. I tried using above mentioned configuration in WCF Service App Config file but it didn’t work.

After some research on this, I found that WCF Services are creating its config at runtime and it’s not picking up the configuration changes that I have set. So as a workaround for this issue we need to set these settings dynamically via Code.

Below is the class file which imports the current runtime as an interface object and set the legacy v2 runtime and returns true or false based on success.

    public static class ActivateLegacyRuntime
    {
        public static bool SetLegacyV2Runtime()
        {
            ICLRRuntimeInfo objICLRRuntimeInfo =
                (ICLRRuntimeInfo)RuntimeEnvironment.GetRuntimeInterfaceAsObject(
                   Guid.Empty,
                    typeof(ICLRRuntimeInfo).GUID);
            try
            {
                objICLRRuntimeInfo.BindAsLegacyV2Runtime();
                return true;
            }
            catch (COMException)
            {
                return false;
            }
        }

        [ComImport]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [Guid("BD39D1D2-BA2F-486A-89B0-B4B0CB466891")]
        private interface ICLRRuntimeInfo
        {
            void xGetVersionString();
            void xGetRuntimeDirectory();
            void xIsLoaded();
            void xIsLoadable();
            void xLoadErrorString();
            void xLoadLibrary();
            void xGetProcAddress();
            void xGetInterface();
            void xSetDefaultStartupFlags();
            void xGetDefaultStartupFlags();

            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
            void BindAsLegacyV2Runtime();
        }
    }

 

Now once we have our class file ready, we just have to call the method SetLegacyV2Runtime and based on what it returns we need to call the Custom assembly method.

If(ActivateLegacyRuntime.SetLegacyV2Runtime)
{
   //Call the assembly Method
}
else
{
  //Show message cannot load the assembly
}

This have worked for me. Hope it works for you too.

Thanks to authors for these articles at below links which helped me in resolving this thing.

http://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/

https://k2recipes.wordpress.com/2017/07/28/mixed-mode-assembly-is-built-against-version-of-the-runtime-cannot-be-loaded/

 

 

(FileLoadException) File Load Exception While calling K2 Methods

While using a class library method which has references to K2 Assemblies we might see a FileLoadException while debugging in Visual Studio as shown in below Screen shot.

fileloadexception

Just use this attribute useLegacyV2RuntimeActivationPolicy=”true” in the app.config <startup> element of the calling application and then rebuild which should fix the issue.

Sample app.config below

<?xml version=”1.0″ encoding=”utf-8″ ?>

<configuration>

<startup useLegacyV2RuntimeActivationPolicy=”true”>

<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.5.2″ />

</startup>

</configuration>