K2

How to generate a Word document from a K2 SmartForm

Generating documents from workflow systems is common.  In fact, record generation in the form of a digital document is a common output of a K2 workflow.  For example, if a service plan is approved in a workflow, a version of the plan is generated using data stored in a database and pushed into SharePoint for storage.

Data in K2 is exposed using K2 SmartObjects.  These are unbelievable flexible objects that allow data access to virtually any data source.  This means we can inject data from lost of different locations in something like a Word document.  This blog step through this process and explains how to generate Word documents using K2 blackpearl and SmartObjects.  In this instance I will show how to generate a document that lists all the users that are part of some departments.

The first step is to have a data source.  In this instance I will use a SmartObject that lists users in Active Directory that is part of the several departments.

generate a Word document from a K2 SmartForm

1 AD User K2 SmartObject

Next, create a Word template that has a table with columns for each of the users’ properties (e.g. User Name, Display Name, etc.).  Each of the columns in the table is populated with a text control from the Developer ribbon.  The key is to copy the SmartObject name in the Title textbox and the SmartObject properties in the Tag textbox.

generate a Word document from a K2 SmartForm

2 Word Template with K2 SmartObject mappings

The next part of the process is to create a OpenXml writer that maps the SmartObject Method and Properties to the controls in the document template.  Some company propriety code is in this component, but the general idea is to create client code that provides inputs to the mapping method.  This give you an idea how such a component can be created

3 Document Generation Code

3 Document Generation Code

The implementation also supports colour coding cells in the table based on data inserted into the cell.  The results are as follows –

4 Document Generated by K2 SmartObjects

4 Document Generated by K2 SmartObjects

From a design perspective, the code above is typically included in a K2 Document Generation Service Object.  This means you can generate a Word document from a K2 SmartForm by calling a K2 SmartObject wired into the K2 Service Object

5 Document Generation using K2 Service Object

5 Document Generation using K2 Service Object

About Velocity

We are K2 Masters.  We are at the cutting edge of K2 workflow, K2 blackpeal and K2 SmartForm development.  Our  projects are changing the way people work across various industries.  For more information, please visit our Velocity homepage.

You may also like

Leave a comment

PODCAST  |  Why you need to establish a K2 Center of Excellence today

Interview with Arno van Rooyen, CEO, Velocity