As I mentioned in my last post, K2 can talk to DLL assemblies. That’s brilliant, because it opens up a whole lot of different APIs to the K2 environment. For example, it could open up old COM assemblies to K2 with some wrappers around the code, or let REST services be linked into K2. More importantly, it lets you bring code directly into your SmartForms and workflows.

So how do we create an assembly that K2 can consume?

It’s really easy. We start with a C# class library.

Now we have a project with an empty class called ‘class1.cs’. Rename the class to ‘HelloWorld’ and your solution should look like this:

Before we get started there’s one simple rule: the methods must be public and static.

Now we can start having some fun. In true tutorial style, let’s build a ‘Hello World’ method.

public static string HelloWorldMethod()
 {
   return “Hello World“;
 }

When we register this class with K2, it interrogates the assembly and extracts all the methods. Each method in the class is treated as a service object, in the same way a table in SQL is treated as a service object in K2. That example is a really simple one, so let’s take it a step further and add a parameter to a method.

public static string WhatIsYourName(string name)
{
  return “Hello World “ + name;
}

As you can see it’s still very simple. Nothing special needs to be added, and no extra libraries need to be referenced.

That’s fine for returning a single value, like a primary key, but what if we want to return more than one value, and more importantly, more than one row of data?

That involves lists. First I’m going to create a simple class to hold my data.

public class MyDetails
    {
        public int Id
        {
            get;
            set;
}
public string Firstname
        {
            get;
            set;
        }
public string Lastname
        {
            get;
            set;
        }
public string EmailAddress
        {
            get;
            set;
        }
}

It’s a simple class with public properties. Now we can build the method that returns the rows of data. We start with a public static method that returns a list of type MyDetails:

public static List<MyDetails> GetUserDetails()

Then we create an instance of that list:

List<MyDetails> result = new List<MyDetails>();

Now we can loop through the data source. In this example we loop through an object that contains user details, and add each row to the list.

foreach(User.detail item in UserDetails.GetDetails())
            {
                result.Add(new MyDetails
                    {
                        Id = item.UserId
                        // Direction = item..ToString(),
                        Firstname = item.forename,
                        lastname  = item.Surname,
                        EmailAddress = item.email

                    });
  }

Put together, it looks something like this:

public static List<MyDetails> GetUserDetails()
{
List<MyDetails> result = new List<MyDetails>();
foreach(User.detail item in UserDetails.GetDetails())
            {
                result.Add(new MyDetails
                    {
                        Id = item.UserId
                        // Direction = item..ToString(),
                        Firstname = item.forename,
                        lastname  = item.Surname,
                        EmailAddress = item.email

                    });
  }
return result;
}

The solution is now complete. Build it and make a note of where the DLL is created.

Telling K2 about the assembly

Now that we have the assembly we can hook it into K2. To do this we use the ServiceObject Tester, which you’ll find in C:\Program Files (x86)\K2 blackpearl\Bin if you don’t know where it is.

Expand the ‘ServiceObject Explorer’, right-click on ‘Endpoints Assembly’ and select ‘Register ServiceInstance’.

Under the service keys, in ‘Assembly Full Path’, enter the complete file location of your DLL and click ‘Next’ and then ‘Finish’.

K2 now maps out all the public static methods it can find in the assembly, and you can build SmartObjects from them.

There are a couple of options. K2 can auto-generate a SmartObject for each method it finds in the assembly, or you can use K2 Studio or K2 Designer to create a custom SmartObject with a SmartObject method for each method K2 found.

Auto-generating the SmartObjects from the tester

  • Right-click on your new service objects and select ‘Create SmartObjects’.
  • Uncheck the ‘System types’ folder.
  • Click ‘OK’.

Your SmartObjects are now created and ready to use in K2. The example code was available to download from the original post.