How to Retrieve Process Instance Reporting Data
Often times the data that appears within the out of box K2 Workspace reports is needed to be accessed directly from some other custom application. The standard K2 reports are based upon a number of SmartObjects that are built in to the K2 product, thus if one needs access to this data, those exact SmartObjects can be used.
The first thing is to know what built in SmartObjects are available and understand what data you are looking for.
You can see the list of SmartObjects via the K2 Object Browser:
So if we wanted to get the data from the Activity Instances Report (as show below):
In this example, to retrieve the data used by this report, the SmartObject we want to use is the "Activity Instances". To code against this SmartObject, the SourceCode.SmartObjects.Client API is used.
Below is a code sample where I have created a method that accepts a SmartObject name and Process Instance ID and retrieves the data. For the purposes of simplicity of this demo, I simply am doing a Response.Write to the ASPX page instead of doing something fancier with the data.
|
private void GetReportData(string strSmartObjectName, int nProcInstID) { SourceCode.SmartObjects.Client.SmartObjectClientServer serverName = new SourceCode.SmartObjects.Client.SmartObjectClientServer(); SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder connectionString = new SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder();
// build a connection string connectionString.Authenticate = true; connectionString.Host = "localhost"; connectionString.Integrated = true; connectionString.IsPrimaryLogin = true; connectionString.Port = 5555;
// open a K2 Server connection serverName.CreateConnection(); serverName.Connection.Open(connectionString.ToString()); try { // get a handle to the SmartObject SourceCode.SmartObjects.Client.SmartObject smartObject = serverName.GetSmartObject(strSmartObjectName); // specify which method will be called smartObject.MethodToExecute = "List"; // specify input parameters for the method smartObject.Properties["ProcessInstanceID"].Value = nProcInstID.ToString(); // call the method SourceCode.SmartObjects.Client.SmartObjectList oSmOList = serverName.ExecuteList(smartObject);
// iterate each smartobject in the collection and do something with the data foreach (SourceCode.SmartObjects.Client.SmartObject oSmO in oSmOList.SmartObjectsList) { // for the purposes of this example, i'm just dynamically building out a string string strRecord = ""; foreach(SourceCode.SmartObjects.Client.SmartProperty oProp in oSmO.Properties) { strRecord += oProp.Name + ": " + oProp.Value.ToString() + ", "; } Response.Write(strRecord + "<br />"); } } catch (Exception ex) { } finally { // close the connection serverName.Connection.Close(); }
} |
When I invoke this method in with the following parameters:
GetReportData("Activity_Instance", 4782);
I see the following (which from a data point of view matches up to the Workspace report):
Obviously this is just a simple example showing how to interact with these SmartObjects. There are other things that one can do with them. For one, if you don't want to write any code you could use the SmartObject .NET data provider to bind the results to a control (perhaps a topic for a later post). You can also integrate these objects into InfoPath forms if desired.
Additionally a very useful way to interact with these SmartObjects (actually any SmartObjects) to get a better understanding of what data and functionality they expose is via the 'Amazing SmartObject Tool' which is freely available on the K2 blackmarket (http://k2underground.com/k2/ProjectHome.aspx?ProjectID=47). I highly recommend this tool for anyone developing or working with SmartObjects.