Building a endpoint assembly for K2 Blackpearl

As I mentioned in my last post, K2 can talk to dll assemblies. Which is brilliant as it opens up a whole lot of different API’s to the K2 environment. So for example it could open up old Com assemblies to K2 with a some wrap arounds the code. It could allow for REST services to be linked into K2. More importantly it allows you to bring code directly into your smartforms and workflow.

So how do we create an assembly that can be consumed by K2?

Well it’s really easy, we start off a with c# class library.

start

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

emptyclass

Before we get started there is one simple rule, that the methods must be public and static.

Now we can start having some fun. So in true tutorial style lets build a ‘Hello World’ method.

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

So when we register this class with K2 it will interrogate the assembly and extract all the methods. Each method in the class will be treated as a service object. In the same way a table in sql is treated as a service object in K2.    The example above is really simple one, so lets take this a step further and lets 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. No additional libraries need to be referenced.

So the above is ok for returning a single value like a primary key, but what if we want to return more than a single value and more importantly what if we want to bring back more than one row of data?

Well it involves Lists. So the first thing i am going to do is  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;
        }

}

So as you can see i have simple class that has public properties.Now we can build the method that will return the rows of data

We start off with  a public static method that returns a list of type MyDetails

public static List<MyDetails> GetUserDetails()

We can then create an instance of that list

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

Now that we have done this we can loop through the data source. In this example we are going to loop through a object that contains user details and for each row i am going to add it to my 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
                        
                    });
  }

If we put this together it will look 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 the solution and make a note of the the location of the dll that has been created.

Telling K2 about the assembly

Now that we have the assembly we can now nook it into K2.  To do this we can use the service object tester. Which if you don’t know where that is you can find it here C:\Program Files (x86)\K2 blackpearl\Bin

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

ServiceObjectTester

Under service keys  – Assembly Full path, enter in the complete file location of your dll and click ‘Next’ and ‘Finish’

AddServiceInstance

K2 will now go and map out all the public static methods it can find in the assembly and you can build a SmartObject from these.

We are given a couple of options to do this, we can get K2 to auto generate a Smartobject for each of the methods it finds in the assembly or we can use one of the tools like K2 Studio or K2 Designer to create a custom Smartobject where we can create Smartobject methods for each of the methods K2 finds in the assembly.

Auto Generate the smartobjects from the tester

CreateSmartobjects

  1. Right click on your newly created service objects and select ‘Create Smartobjects’
  2. Uncheck ‘System types’ folder
  3. Click ‘OK’

Your Smartobjects are now created and can be used by K2.

You can download the example code from here

6 thoughts on “Building a endpoint assembly for K2 Blackpearl

    1. Yes you could but K2 also comes with couple of brokers that can be used with web services.
      There are the following brokers
      1. Endpoints REST, for creating SmartObjects from REST Services
      2. Endpoints WCF, for creating SmartObjects from WCF Services
      3. Endpoint web services which can be used for asmx web services

      Like

Leave a comment