asp.net-mvc-4workflowk2

How to Invoke K2 workflow from MVC


All Just wondering if by any chance can we invoke K2 workflow from controller? I have a UI developed in MVC and need to implement some workflow... Is there any way we can achieve this ? Are there any K2 APIs ? we can call directly from controller ? Any pointers, example will help.

Thanks in advance.


Solution

  • You can most certainly do anything with K2 from MVC. They have a full range of APIs (web services, dlls, etc.). You can view the developers reference here.

    Your K2 installation contains all the *.dll's you need. The default location is C:\Program Files (x86)\K2 blackpearl\Bin.

    Here is a simple example of starting a workflow using the SourceCode.Workflow.Client.dll: (NOTE: I write my own class libraries to handle all of my K2 interactions, to separate the work OUT of my controllers, but you could simply place the method below in your controller if you wanted).

    using SourceCode.Workflow.Client;
    
    public class MySampleK2Service: IMySampleK2Service
    {
    
        private readonly string serverHost;
        private readonly string impersonatedUser;
    
        public MySampleK2Service(string serverHost, string impersonatedUser)
        {
            this.serverHost = serverHost;
            this.impersonatedUser = impersonatedUser;
        }
    
        public int StartNewWorkflow(string processName, string folio)
        {
            using (var connection = new Connection())
            {
                connection.Open(this.serverHost);
    
                if (this.impersonatedUser != null)
                {
                    connection.ImpersonateUser(this.impersonatedUser);
                }
    
                var processInstance = connection.CreateProcessInstance(processName);
                processInstance.Folio = folio;
                connection.StartProcessInstance(processInstance, true);
    
                return processInstance.ID;
            }
        }
    }