July 2, 2009 1 Comment
How does K2[blackpearl] integrate with other systems? One standard way to do this is through Web Services. And how does K2[blackpearl] integrate with Web Services? It is done through SmartObjects. So we have: K2 SmartObject Web Service Other System
“A K2 SmartObject can encapsulate data that does not currently reside in an existing system, such as data about an employee’s extracurricular activities, his or her family, or hobbies and interests. By using a SmartObject for employee information, all data about an employee is accessed in the same place, and updates are applied to the data at its source. It is important to remember that SmartObjects do not store or cache data. Once created, a SmartObject can be used and reused in a variety of ways; within K2 Designer for Visio, integrated for use in an InfoPath form, a workflow deployed via K2 Studio, in a report, in SharePoint, etc.”
SmartObject is all that and also it is the link between K2 and Web Services. So let’s see step by step how do we consume a Web Service method.
1. Create Web Service
First, let’s create a simple web service which we will be consuming later. For the purposes of the demonstration, it will be as simple as returning you the sum of two values.
For the curious, it returned 3 ;)
‘Publish’ it somewhere on your local hard drive and make sure you map it in IIS to a virtual directory. You can do that by going into IIS, choose existing site (or create a new one), right click on it -> New Virtual Directory, browse to the path where you Published the service and keep clicking Next.
Let’s assume you published your service to http://localhost:86/SampleService/SampleService.asmx, which in the local network (if you have one) maps to http://192.168.0.1:86/SampleService/SampleService.asmx.
We are done with the service, it’s in the network, it works, it is ready to be registered as a service instance.
2. From K2 Workspace add it to Dynamic Web Service
Ok, now we have to add it to the ‘Dynamic Web Services’ list. Open your K2 Workspace. Go to Management -> Management Console -> Expand the servername -> SmartObjects -> Services -> Dynamic Web Service.
You have now registered a service instance. Let’s verify that by going to the K2 server and opening C:\Program Files\K2 blackpearl\ServiceBroker (or C:\Program Files (x86)\K2 blackpearl\ServiceBroker) and run BrokerManagement.exe. On the first screen, click on ‘Configure Services’.
Under services -> DynamicWebService -> serviceinstances there should be an instance with a name ‘SampleService‘.
Click Next. On the next screen you have to select the Service Method we will be executing. This is easy. First click Add, to start adding a service method call to the SmartObject. A new popup will show. There is a field ‘Service Object Method’ and next to it there is a ‘…’ button. Click on the button. It will open the Context browser. Browse through ServiceObjects Server -> Sample Service -> Sum -> Read.
We need to map some fields now. See the ‘a’ and ‘b’ input properties? They are the web service method input parameters. We have to ‘bind’ them to the intA and intB fields we created for the SmartObject.
Then click ok and do the same thing for ‘b’, but select ‘intB’ instead. For ‘ReturnValue’ – click on it, Assign, set ‘Map To’ to SmartObject Property (i.e. you are storing the result in a property of the SmartObject). Then click Create.
Right click the solution and do Rebuild. It should compile with no errors. Let’s deploy the SmartObject to our K2 Server. Right click the project -> Deploy. Follow the wizard (Next, Finish) and your SmartObject should be deployed.
Let’s test your SmartObject. Log into the K2 server and go back to C:\Program Files (x86)\K2 blackpearl\ServiceBroker. Run the file SmartObject Service Tester.exe.
We just invoked the SmartObject, which talked to the WebService, retrieved result for us and was visualized in the test utility. Everything works perfectly, your SmartObject is ready to be consumed from a workflow!
From the Toolbox, drag and drop a new SmartObject event. On the second page, Wizard will prompt you for SmartObject Method. Browse to our Sum method in the SampleServiceSmartObject.
Click Add. It should look like this:
On the next screen, you have to provide values for the SmartObject input properties. This could be K2 DataFields from your process, or actual hardcoded sample values. For the purpose of the demo, we will just bind them to ’2′ and ’54′. Select the property, click Assign button, set value, click ok. It should look like this:
On the next screen, we have to bind the return result to something. Let’s bind it to a DataField in the process which we will create. Click on the ReturnValue, click ‘Assign’, in the popup click on the ‘…’ button which will open the Context Browser. In this browser, either select already existing DataField, or create a new one on the fly (right click -> create). Make sure the field is Integer (for this demo). Select it, click ‘Add’, then ‘Ok’ in the pop up.
Click finish. You just finished adding a SmartObject event to your workflow. After this event is executed, you will have the value of ‘a’ + ‘b’ in the SumValue data field. I.e. you are consuming the SmartObject (and hence the Web Service).
Now you just need to finish the rest of the workflow ;)
7. Change the URL of the service
What if the address of your Web Service changes? Simple, go to the K2 server and open the C:\Program Files (x86)\K2 blackpearl\ServiceBroker folder. Run the BrokerManagement.exe app. On the first screen, click on ‘Configure Services’.
And that’s it, your service instance is now pointing to the right URL.
Basically that’s all. Now you know how to consume web services from your K2 Workflows, utilizing SmartObjects!
Hope this helps!