Part 1: The API and listing forms and views
For the last six months I have been working on a really big K2 project, probably one of the biggest that has happened in the UK, with multiple teams working on the same environment and collaborating to build a solution for the client.
One thing that struck me was missing from the K2 platform was the ability to see which artefacts (forms, views, SmartObjects and workflows) made up a solution. Yes, I could go into edit mode on each form or view and see which components were being used, but that takes up a lot of time. What I wanted was a way to click on a view and see which SmartForms it was being used in.
So this got me thinking about what I could do that wouldn’t involve too much hard work. I could look in the K2 database, but as we all know we aren’t really allowed to tinker around with that. I googled SmartForms explorer and inspector tools, and there were some off-the-shelf options where I could navigate through the forms. But that wasn’t really what I wanted. I didn’t want to pay for a tool, and if I could see this information inside a SmartForm, that would be even better. So I started looking at the SmartForms API that comes out of the box with K2 SmartForms.
The API
It may not be brilliantly documented, but with a little research and messing around there is a lot you can do with this API, from exploring the contents of a view or form to dynamically adding parameters and controls to your artefact.
The assemblies I will be using are:
- SourceCode.Forms
- SourceCode.Forms.Management
- SourceCode.Forms.Authoring
Hello world
OK, let’s start with a basic example. We will retrieve a list of all the forms on the K2 server.
public List<SmartFormView> GetAllForms()
{
List<SmartFormView> list = new List<SmartFormView>();
FormsManager frm = new FormsManager(servername, port);
FormExplorer formexplorer = frm.GetForms();
foreach (SourceCode.Forms.Management.FormInfo forminfo in formexplorer.Forms)
{
list.Add(new SmartFormView
{
name = forminfo.Name,
displayname = forminfo.DisplayName,
description = forminfo.Description,
guid = forminfo.Guid,
version = forminfo.Version,
CheckedOutBy = forminfo.CheckedOutBy,
theme = forminfo.Theme.ToString()
});
}
return list;
}In the code above we first create a list to hold the details of each form. Then new FormsManager(servername, port) tells the API which K2 server to look at and makes a connection to it. Once we have a connection, frm.GetForms() gets a list of all the forms on the server. All we do then is loop through the forms and, for each one, read its properties: name, description, version and so on.
Getting a list of all the views
We can do the same with views. The code is almost identical; the main difference is that it calls frm.GetViews() and uses the view version of the explorer object (ViewExplorer and ViewInfo) rather than the form version.
Part 2: Finding forms for a view, and views for a form
In part 1 we looked at a couple of basic methods for getting a list of the SmartForms and views on the K2 server, which can be really handy if you want to build a library of all your forms and views.
But can we take this further? From the K2 designer we can already expand a form and see which views make it up, but we can’t click on a view and see which forms it is used in.
Finding all forms for a selected view
This uses pretty much the same code as before, except this time we use some additional methods that come with the API.
public List<SmartFormView> GetAllFormsForView(string viewName)
{
var list = new List<SmartFormView>();
var frm = new FormsManager(servername, port);
FormExplorer formexplorer = frm.GetFormsForView(viewName);
foreach (SourceCode.Forms.Management.FormInfo forminfo in formexplorer.Forms)
{
list.Add(new SmartFormView
{
name = forminfo.Name,
displayname = forminfo.DisplayName,
description = forminfo.Description,
guid = forminfo.Guid,
version = forminfo.Version,
CheckedOutBy = forminfo.CheckedOutBy,
theme = forminfo.Theme.ToString()
});
}
return list;
}It’s practically the same as looping through all the forms. The only difference is the call to frm.GetFormsForView(viewName). We pass in the name of the view and can then loop through all the forms where that view is used.
Finding all views for a selected form
Since we have done this for the view, it’s only fair to do it for the form. Again the code is the same as the part 1 view example, except it calls frm.GetViewsForForm(formname). You pass in the name of the form (or even its GUID) and it returns a list of all the views in that form.
Other methods
We can take this even further and get the views used by a SmartObject, and even the views and forms calling a specific workflow:
- GetViewsForObject(SmartObject GUID)
- GetViewsForProcess(Process Name)
- GetFormsForProcess(Process Name)
Part 3: Parameters, controls and properties
Now we go deeper into those artefacts without having to open them in edit mode to see what’s going on.
To do this we need some objects that are not in the standard properties from part 1. We need access to the definition of the artefact (basically the XML). This gives us access to its properties, controls, parameters and events. For a SmartForm and a view respectively, we use:
SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(‘form name’));
SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(‘View Name’));Parameters
Let’s start with an easy one and get the list of parameters on a form.
public List<SmartFormViewParameters> ArtifactParameters(SourceCode.Forms.Authoring.FormParameterCollection parameters)
{
List<SmartFormViewParameters> list = new List<SmartFormViewParameters>();
foreach (SourceCode.Forms.Authoring.FormParameter parameter in parameters)
{
list.Add(new SmartFormViewParameters {
name = parameter.Name,
type = parameter.DataType.ToString(),
defaultvalue = parameter.DefaultValue
});
}
return list;
}The method takes a parameter collection, which we get using the definition code above, for example ArtifactParameters(form.parameters). We then loop through all the parameters in the collection and read each one’s name, type and default value.
Controls
Getting the controls works the same way. We pass in the controls collection from the artefact and loop through it, reading each control’s name, type, GUID and properties.
public List<SmartFormViewControls> ArtefactControls(SourceCode.Forms.Authoring.ControlCollection controls)
{
List<SmartFormViewControls> list = new List<SmartFormViewControls>();
foreach (SourceCode.Forms.Authoring.Control control in controls)
{
list.Add(new SmartFormViewControls {
name = control.Name,
type = control.Type,
guid = control.Guid,
properties = control.Properties
});
}
return list;
}The control method works for both SmartForms and views, and it brings us nicely to the properties object.
Properties
A property method can be used to get the properties of a control, such as its width or field, and also the properties of a view, a SmartForm or even a rule. It accepts a PropertyCollection and loops through it in the same way, reading each property’s name (for example ‘Width’) and value (for example ‘200px’).
Part 4: Rules
Next we look at how to get the rules from forms and views. This is probably the most complex part of the API, as it takes a number of different methods to get the information. We use the same definition code as in part 3, which gives us access to the Events object.
Events
Most rules in SmartForms and views start with an event. An event is when something happens, such as the click of a button.
public List<SmartFormViewEvents> ArtefactEvents(SourceCode.Forms.Authoring.Eventing.EventCollection events)
{
List<SmartFormViewEvents> list = new List<SmartFormViewEvents>();
foreach (SourceCode.Forms.Authoring.Eventing.Event ev in events)
{
if (ev.SourceType == SourceCode.Forms.Authoring.Eventing.EventSourceType.Rule)
{
list.Add(new SmartFormViewEvents
{
name = ev.Name,
type = ev.EventType.ToString(),
handlers = ev.Handlers,
properties = ev.Properties
});
}
}
return list;
}This gives a list of all the events on the artefact. We loop through the event collection and check whether the event’s source type is Rule. From each one we get its name, type, handlers and properties. The important one is Handlers, as that gives us access to the conditions and actions of the rule.
Handlers
The handler object is where we drill down into the rule. We pass in the handler collection from the event, loop through all the handlers for that event, and for each one get its name, conditions and actions.
public List<SmartFromViewHandlers> ArtefactHandlers(SourceCode.Forms.Authoring.Eventing.HandlerCollection handlers)
{
List<SmartFromViewHandlers> list = new List<SmartFromViewHandlers>();
foreach (SourceCode.Forms.Authoring.Eventing.Handler handle in handlers)
{
list.Add(new SmartFromViewHandlers
{
Actions = handle.Actions,
Conditions = handle.Conditions,
Name = handle.HandlerType.ToString()
});
}
return list;
}Conditions
Conditions check whether something is true, for example ‘all the required fields are entered’. The method loops through the handler’s ConditionCollection and reads each condition’s properties.
Actions
So we can access the event and the conditions. Now let’s look at the actions of a rule.
foreach (SourceCode.Forms.Authoring.Eventing.Action action in actions)
{
list.Add(new SmartFormViewActions
{
properties = action.Properties,
parameters = action.Parameters,
results = action.Results,
validation = action.Validation,
viewguid = action.ViewGuid,
method = action.Method,
formguid = action.FormGuid,
executiontype = action.ExecutionType.ToString(),
controlguid = action.ControlGuid,
actiontype = action.ActionType.ToString()
});
}This gives us the complete information about the action, and it opens up some more objects we can explore. The most useful are the action’s parameters and results (both MappingCollections, giving the source and target type, path, ID and value of each mapping), its validation result (status and messages) and the validation messages themselves. The full methods for each of these are in the original post.
Part 5: Building the spider library
Now we take the methods from parts 1 to 4 and build a complete assembly. The SmartForm spider library is what we use to create an endpoint assembly, so we can create SmartObjects from it and eventually SmartForms.
Getting started
- Open Visual Studio and create a class library project
- Add references to SourceCode.Forms.Management, SourceCode.Forms.Deployment, SourceCode.Forms and SourceCode.Forms.Authoring
- Create a class called Common.cs. This contains all the common classes that hold the form and view data: SmartFormView, SmartFormViewProperties, SmartFormViewControls, SmartFormViewParameters, SmartFormViewEvents, SmartFromViewHandlers, SmartFormViewActions, SmartFormViewActionParameters and the validation classes. They are plain classes with get/set properties matching the fields we read in parts 1 to 4.
- Create another class called Properties.cs, containing the generic ArtefactProperties method from part 3 for getting the properties of an item
The SmartForm object
Next, create a class called Smartform.cs. This contains all the public static methods for getting key information from a SmartForm, and it’s the object K2 will use as a service object. Because the methods are static and called one at a time by K2, each one takes simple inputs (the form name, and the GUIDs of the event, handler or action) and loads the form definition itself. Here is how it drills down from a form to the handlers of one of its events:
/// <summary>
/// Event Handlers
/// </summary>
/// <param name=”EventGUID”></param>
/// <returns></returns>
public static List<SmartFromViewHandlers> FormHandlers(String FormName,Guid EventGUID)
{
List<SmartFromViewHandlers> list = new List<SmartFromViewHandlers>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));
var ev = form.Events[EventGUID];
SourceCode.Forms.Authoring.Eventing.Event e = form.Events[EventGUID];
foreach (SourceCode.Forms.Authoring.Eventing.Handler handle in e.Handlers)
{
list.Add(new SmartFromViewHandlers
{
Name = handle.HandlerType.ToString(),
GUID = handle.Guid
});
}
return list;
}The other methods follow the same pattern: LoadForm, GetAllForms, GetAllFormsbyView, GetFormProperties, FormParameters, FormControls, FormEventsEvents, ArtefactConditions, ArtefactActionss, and the action parameter, result and validation methods. Conditions and actions go one level further with form.Events[EventGUID].Handlers[HandleGUID], and action parameters and results go to .Actions[ActionGUID].
The view object
Then create a class called View.cs. Like Smartform.cs, it contains all the methods for getting information about views on the K2 environment.
The full listings for Common.cs, Smartform.cs and View.cs are in the original post.
Now we can build the solution, and we are ready to tell K2 about it, make it a service object and then a SmartObject.
Creating the service object
Creating the service object is really easy. Open the SmartObject Service Tester.
- Right-click on Endpoints Assembly and click ‘Register Service Instance’
- In ‘Assembly Full Path’, enter the full path to the assembly’s DLL
- Click Next and enter ‘Form Spider’ as the Display Name and System Name
- Click Finish
K2 will then pick up the DLL and map out all the public static methods.
Now that we have the service objects, we can create the SmartObjects. For this project I have created just two, one for SmartForms and one for views. Each contains all the public static methods we created in the class library. Below is an example of one.
Part 6: The Form Spider views and forms
So we have looked at the API, built the endpoint assembly and turned it into a couple of SmartObjects. Now we can build some views and SmartForms to show the data.
SmartObject recap
We have two SmartObjects: Spider.smo.Views, which looks at the details of a view, from its basic details to its rules, and Spider.smo.Smartform, which does the same for a form.
The views of Form Spider
Most of the views will be list views, as the methods return multiple rows of data. The only two that aren’t are the ones that let us search for a specific SmartForm or view. Each of these item views contains the artefact name (a picker control), the description (a data label) and the version (a data label).
Each item view has the same rule: when the picker control changes, we transfer the description, version and so on from the selected row into the view’s data labels.
List views
Each list view has a ‘ViewName’ or ‘FormName’ parameter, depending on whether it’s for a form or a view. This lets us pass in a form or view name and load, for example, the parameters for that artefact.
There is also a view showing all the properties of the artefact, and views for the views in a form and the forms that contain a certain view.
The controls view shows all the controls on the artefact, including hidden ones such as table properties and user controls. Clicking on a control in the list brings up a pop-up showing all its properties.
There is also a view showing all the events on the artefact. Clicking on an event shows all the conditions and actions for it.
SmartObjects
Using the Form Spider assembly, we can also get the details of SmartObjects, such as their properties, methods and the views using that SmartObject. We could take this even further by getting the details of the service object, but for this version I have kept to the basic details.
Workflow
As with SmartObjects, we can use Form Spider to look at the workflows on the K2 server too. The current version shows basic details about the workflow, such as its description and number of versions. It also shows the process data fields and XML data fields, the activities that make up the workflow, and which SmartForms and views are linked to it.
Clicking on an activity also shows the events that make up that activity.
The original post finishes with a video demo of Form Spider.