Thursday, July 7, 2016

K2 Interview Questions : K2 extensions/API, Part 1


Scenarios where K2 Workflow Client API (SourceCode.Workflow.Client.dll) can be used.
The workflow Client API refers to the .NET assembly SourceCode.Workflow.Client.dll. This assembly exposes several classes and methods that are used by developers when interacting programmatically with a deployed workflow.

In practice, the SourceCode.Workflow.Client assembly is often used to create custom task lists, to start workflows or complete workflow tasks (known as worklist items) from custom user interfaces like ASP.NET web pages, and to create alternative interfaces or tools to allow users to maintain their out-of-office status and worklist items. The workflow client is also often used to create automated testing code or applications that are executed against a workflow definition to verify that the workflow is behaving as expected.

Scenarios where K2 Workflow Management API is used.
The workflow Management API refers to the .NET assembly SourceCode.Workflow.Management.dll. This assembly exposes several classes and methods that are used to administer K2 workflows and the K2 environment. Essentially, this API allows developers to perform similar administration functions as the ones exposed on the K2 workspace and K2 process portals’ Management console pages. The Workflow.Management API is not exposed as a WCF or Web service: it is only available as a .NET .dll
In practice, the SourceCode.Workflow.Management assembly is often used to manage processes, automate Error reporting and Error repair, to manage other users’ worklist items and Out-of-Office statuses, to manage active process instances and perform workflow version migration for in-flight workflows.



K2 Interview Questions

SmartObject

Part 1 (Beginner)
When NOT to use SmartObjects?
What are the Common uses for SmartObjects?
What are the standard K2 service broker/Connectors to LOBs?

Workspace
Part 1 (Intermediate)
What are Worklist Item/task statuses?

K2 Extensions/API
Part 1 (Adv)

K2 Workflows
Part 1 (Adv)
Scenarios where K2 Workflow Client API (SourceCode.Workflow.Client.dll) can be used.
Scenarios where K2 Workflow Management API is used.
Part 2 (Adv)
How do you fix the process in errors?
How do you debug the process if  the Visual studio is not installed on the K2 box?

Thursday, June 30, 2016

For each loop processes half of the items in K2 workflow.


Scenario 
In my scenario, I have SharePoint list hooked up with smartobject. SharePoint list has flag column (Yes/No), default is set to No. All the items are set to No at the beginning. 

For each loop is set to retrieve 1 item at a time. Then it is doing some operations on that item. Then finally, I am setting flag column to Yes, indicating that item has been processed.


Issue
After updating the flag to Yes, next, loop is retrieving only odd number item from a list, and skipping even number items (basically, it is processing only half of times in the list or only alternate items).

Without update operation, it works fine.

Workaround

Decreasing the index of the for-each loop after the for-each event loop processes all the items.


Thursday, June 23, 2016

K2 Redirect Other User's Tasks Using Smartfom. No code solution!!


We have several K2 Applications. Each has application coordinator. Below are the common scenarios when Application coordinator contact technical team to redirect the task.

1. When a user is on vacation.
2. Somebody left the organization and bunch of the tasks that user has not completed


Unfortunately, to redirect the task you will need server admin rights in the workspace. Though you have process admin rights, k2 doesn't allow to browse the worklist for that process and redirect the task. I don't want to give application coordinator server admin rights and put my all processes/ configuration etc in jeopardy.

With below solution, application coordinator can redirect the multiple tasks simultaneously for the processes they have process admin access on it.
































Below is step by step demo

1. Application coordinator selects the process in dropdown Select workflow. (dropdown only shows processes for which user has admin rights on process and it is configurable. You can also give access to users who don't have access to any process) it will load the grid with all the active tasks.

2. Then select the user you want to redirect the task to.

3. Select one or multiple tasks in a grid and click on redirect button.

There you go, you have redirected the tasks without having any access to K2.

If you need the solution, please add your email in comments below. I will send you the solution.


Releasing K2 Running Processes

Several times, especially in dev environment some of the processes stuck in running mode. It might be the infinite loop you coded or any glitch from the server.
When you try to stop or delete them from the workspace, workspace keeps spinning till death. It keeps eating up the processor and slows down the server, how to get rid of these process?

First, try to restring the K2 service hoping the running processes unstuck. Most of the times it doesn't work and you need to follow below steps.

Stop the K2 host service.

Go to the K2 database. K2 recommends not to touch their database, but in this case, I don’t see any other option.

You can find running processes with below query. We are on 6.7.11 version of K2.
SELECT * FROM [K2].[Server].[ProcInst] where Status = 1 

Here is a link which explains the K2 database ERD diagram. 

On the bottom of the pdf, there is an explanation of several flags used in the database. I found this pdf very useful while custom development.
Below are Process Status flags and its explanation.
















Run below query for each running process to delete the process. You can also set the process status to stop them, in my case, It did not work and anyway, these are the faulty process and you would want to get rid of them.
Update [K2].[Server].[ProcInst] set status= 1 where ProcID =1234 and ID=5678

Wow, now it is working like a charm. The server went back to its normal status.

Monday, June 29, 2015

K2 Interview Questions: SmartObjects

All K2 Interview Questions

When NOT to use SmartObjects
Very high-volume or high-load scenarios
(e.g. high-volume OLTP)
- Very low-latency processing requirements
(e.g. real-time control systems, ultra-low latency trading, guaranteed millisecond execution times)
- Enterprise Data Model Design
(SmartObjects is an execution mechanism and implementation more suited for execution rather than design)
- Message Bus/Transformation Engine for transactional data processing or ETL
              A good rule to apply is to use the approach that makes the most sense. Using SmartObjects to allow workflows to integrate with external providers does not imply that you have to use SmartObjects for Forms and Reports as well. You could define SmartObjects and use them in workflows where response times are not as critical, but use native data access in user interfaces when very low latency is required.

What are the Common uses for SmartObjects
Data access in forms and reports
- Primary data integration mechanism for K2 smartforms
- Interact with external systems from workflows
- Interface between K2 SmartWizards and data sources
- Standardize data access mechanisms for development projects
- Create a catalog of reusable business entities
- Combine disparate data sources into logical, abstracted business entities
- Externalize workflow business rules
- Dynamic rules in workflows (destinations, start rules etc.)
- Expose SmartObjects on SharePoint lists using BCS
- Expose disparate external systems through standard APIs (e.g. K2 APIs and Web Services)

Expose SmartObjects on SharePoint lists using BCS
It has been possible to expose K2 SmartObjects on SharePoint lists using SharePoint’s Business Connectivity Services (BCS) technology. This makes it possible to expose almost any data source as a read or read-write SharePoint list.

Expose disparate external systems through standard APIs (e.g. K2 APIs and Web Services)
The K2 SmartObjects Services and the available K2 SmartObject APIs also make it possible to expose almost any data source consistently through the same interfaces. Imagine how much easier it is for developers to write code against a consistent API, regardless of the source of the data, rather than having to learn different APIs and access methods for different data sources. 

What are the service broker
The connector which exposes a data provider to the SmartObject subsystem is called a “Service Broker”. These service brokers are hosted in the K2 application server and are responsible for translating the provider’s native objects, properties and methods into the common “language” that K2 SmartObjects can understand. K2 ships with several Out-of-The-Box Service Brokers that can expose common enterprise systems as SmartObjects without requiring any code. At most, it is only necessary to configure the providers to access a specific data source, after which SmartObject developers can create logical SmartObjects that interact with the data source.

What are the standard K2 service broker/Connectors to LOBs
Microsoft Products
 - Active Directory (Write and Read)
 - SharePoint (On-premise and Online)
- K2 user Role M anagement
- SQL Server databases
- SQL Server Reporting Services
- Exchange Server
- SmartObject Service Description
- Dynamics CRM (On-Premise and Online)
K2 Services
- K2 SmartBox
- K2 Task Allocation
- K2 Workflow Reporting (Generic and Exchange Server Solution-specific)
- K2 User Role Management
- SmartObject Service Description
- K2 Workflow Runtime Interaction
Other Products
- Salesforce_corn
- Oracle databases
- SAP (When using K2 connect)
Generic Brokers
- .NET Assemblies
- WCF Services
- Web Services
- PDF Converter

Sunday, June 21, 2015

K2 BlackPerl REST API : Get Count Of Tasks Assigned To User.

Scenario -
In K2 worklist webpart, it lists the tasks assigned to user but not the count or number of task assigned to user. It would be very useful for the users to know how many tasks are pending to action at a glance. 
Our goal is to get the number of tasks assigned to user for particular K2 application.

K2 REST Services -
K2 Services REST endpoints provide standard URIs for interacting with the available data and operations. 

There are five REST service provided by K2, for now.
  1. Core Service - The Core service is useful for ensuring a connection can be established to the services and determining the current user context making the call.
  2. Identity Service - The Identity service is useful for finding users that the K2 Server has access to; for example, when the user wishes to redirect an item to another user.
  3. Task Service - The Task service is useful for determining what outstanding tasks a user has, as well as batch-actioning tasks that a user might have actioned while they were offline.
  4. Worklist service - The Worklist service is useful for interacting with worklist items.
  5. Process Service - The Process service is useful for interacting with process definitions and process instances

K2 has good documentation on these services with example.

Back to our main business - 
I used the ajax call to the Task services and used the length function to get the tasks count.
It's important to use "Cache:false" parameter in ajax call as in IE it cache the count number, so each-time when page refresh it should get the correct count number.

You can filter the number of tasks to be return for particular process/for all processes which are inside particular folder.

Here is how you can define the filter using the 'filterXml' as query string parameter

url: 'http://[server:port]/K2Services/SyncREST.svc/Task/Items?filterXml=<Criteria xmlns="http://schemas.k2.com/worklist/d1"><Filter Field="ProcessFolder" Comparison="Equal" ValueType="String">[solution/Project name]</Filter></Criteria>'


When you deploy the K2 project, in workspace it becomes a folder and you can expand it to see all processes in that solution/project.

In my case, I had to get the count for all the processes inside the folder for a user.




















There are good number of options available to filter the tasks, for information please check the K2 documentation.
Output:





Finally, here is working code which gives the number of tasks assigned in all process in a particular folder.
Code:

<Html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var tasks;
function GetMyTasksCount() {
    $.ajax({
        // Replace the [server:port] and filter criteria [solution/Project name] as per your requirement
        url: 'http://[server:port]/K2Services/SyncREST.svc/Task/Items?filterXml=<Criteria xmlns="http://schemas.k2.com/worklist/d1"><Filter Field="ProcessFolder" Comparison="Equal" ValueType="String">[solution/Project name]</Filter></Criteria>',
        method: 'GET',
        crossDomain: false,
cache: false,
        error: function (data, error, status) {
$("p:contains('My Tasks')").append(" "+"(cannot retrieve count)");
        },
        success: function (data) {
            tasks = data;
            ShowMyTasksCount();
           
        }
    });
}

function ShowMyTasksCount() {        
    $("p:contains('MY TASKS')").append(" "+"(" +$(tasks).find("Task").length+")");
}

/* Get the worklist when the page loads */
$(document).ready(function () {
    GetMyTasksCount();

});
</script>
</head>
<body>
<p>MY TASKS</p>
</body>
</html>