Building a K2 Smartform spider part 6

So we have looked at the API and we have made the endpoint assembly and this into a couple of SmartObjects. Now that we have these  we can now  build some View and Smartforms to show the data on the Smartforms.

SmartObject ReCap

So we have two SmartObjects one for the Views and one for the Smartforms.

Spider.smo.Views

Looks at details of the view from it’s basic details to it’s rules

View.smo

Spider.smo.Smartform

Looks at details of the view from it’s basic details to it’s rules

form.smo

 

The views of FormSpider

VIEWSPIDER

Now we can build some views so we can interact with the SmartObjects. Majority of the views will be list views as the methods return back multiple rows of data. The only two that are not going to be list views will be the two forms that allow us to search for a specific Smartform or view.

 

Each of the items views contains the following fields

  1. Artifact name (picker control)
  2. Description (data label)
  3. Version (data label)

view

formheader

Each of the item views has the same rule, which is when the picker control is changed we then transfer the description, version etc.. from the selected row into the view data labels.

pickerchanged

List Views

Parameters

Each of the list views will have the parameter of ‘ViewName’ or ‘FormName’ depending on whether it’s a form view or view view. This allows us to pass in a form name or view name and then load up the parameters for that arifact.

parameters

Properties

We also have view to show all the properties of the artifact

properties

Views in a form and forms that have a certain view in

viewsinform

Controls

The control views shows all the controls on the artifact, whether they are hidden controls such table properties and user controls.

formcontrols

By clicking on a control in the list,  there will be a pop up that show’s all the properties of that control.

CONTROLPROP

Rules

There is also a view to show all the events on the artifact

rules

By clicking on an event,  we are then shown  all the conditions, actions for that event

viewrules

 

SmartObjects

Using the Form Spider assembly, we can also access the details of the SmartObjects, such as its properties, methods and the views the being used by that SmartObject.

SmartObjectsSpider

We could take this even further by getting the details of the service object, but for this version I have just kept it with the basic details

Workflow

Like with the SmartObjects we can also use Form spider to have a look at the workflows on the K2 server as well.

The current version will show the basic details about the workflow such as its description, number of versions etc..

It also shows the process data fields and XML data fields, the activities that make yo the workflow and also  what Smartforms and Views are linked to that particular workflow.

workflowspider

Also by clicking on activity we also get to see the events that make up that activity as well.

workflowevents

Below is a video that goes through the  a demo of Form Spider

Building a K2 Smartform spider part 5

In parts 1 to 4 we looked at some of the different classes and methods that are available in the Smartform API. In part 5 we will be taking these methods and building a complete assembly for us to use.

Smartform spider library

The Smartform spider library is what we will use to create an endpoint assembly so we can create SmartObjects from it and then eventually Smartforms.

Getting Started

1. Open up Visual studio and create a class library project

2.Add the following references to the library

using SourceCode.Forms.Management;
using SourceCode.Forms.Deployment;
using SourceCode.Forms;
using SourceCode.Forms.Authoring;

 

3.Create a class called ‘Common.cs’, this will contain all the common classes to hold the form and view data.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudFish.FormSpider
{
public class SmartFormViewSml
{
public string name
{
get;
set;
}
public string displayname
{
get;
set;
}

public string description
{
get;
set;
}

public Guid guid
{
get;
set;
}
public int version
{
get;
set;
}
}

public class SmartFormView
{
public string name
{
get;
set;
}
public string displayname
{
get;
set;
}

public string description
{
get;
set;
}

public Guid guid
{
get;
set;
}
public int version
{
get;
set;
}

public string theme
{
get;
set;
}

}

public class SmartFormViewProperties
{
public string name
{

get;
set;
}

public string value
{

get;
set;
}
}
public class SmartFormViewControls
{
public string name
{
get;
set;
}

public string type
{
get;
set;
}

public Guid guid
{
get;
set;
}

}

public class SmartFormViewParameters
{
public string name
{
get;
set;
}

public string type
{
get;
set;
}

public string defaultvalue
{
get;
set;
}
}

public class SmartFormViewEvents
{
public string name
{
get;
set;
}

public string type
{
get;
set;
}

public Guid GUID
{
get;
set;
}
}
public class SmartFromViewHandlers
{

public string Name
{

get;
set;
}

public Guid GUID
{

get;
set;
}

 

public Guid GUID
{

get;
set;
}

}

public class SmartFormViewActions
{
public Guid GUID
{

get;
set;
}

public Guid viewguid
{
get;
set;
}

public Guid formguid
{
get;
set;
}

public string method
{
get;
set;
}

public string executiontype
{
get;
set;
}

public Guid controlguid
{
get;
set;
}

public string actiontype
{
get;
set;
}
}
public class SmartFormViewActionParameters
{

 

public string targettype
{

get;
set;
}
public string targetpath
{

get;
set;
}
public string targetid
{

get;
set;
}

public string sourcevalue
{

get;
set;
}

public string sourcetype
{

get;
set;
}
public string sourcepath
{

get;
set;
}

public string sourceid
{

get;
set;
}
}
public class SmartFormViewActionValidation
{

public string status
{
get;
set;
}
}

public class SmartFormViewActionValidationMessage
{

public string message { get; set; }
}

}

 

4.Create another class called ‘properties.cs’, this will contain a generic method to get the properties of an item.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudFish.FormSpider
{
class Properties
{

public List<SmartFormViewProperties> ArtefactProperties(SourceCode.Forms.Authoring.PropertyCollection properties)
{
List<SmartFormViewProperties> list = new List<SmartFormViewProperties>();
foreach (SourceCode.Forms.Authoring.Property prop in properties)
{
list.Add(new SmartFormViewProperties
{
name = prop.Name,
value = prop.Value,

});

}
return list;
}
}
}

SmartObject Object

5.Now lets create a class called ‘Smartform.cs’ this class will contain all the public static methods for accessing key information from a Smartform and is the object that will be used as a Service Object by K2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SourceCode.Forms.Management;
using SourceCode.Forms.Deployment;
using SourceCode.Forms;
using SourceCode.Forms.Authoring;

/// <summary>
/// Explores the content of the SmartForm
/// </summary>
namespace CloudFish.FormSpider
{
public class Smartform
{

/// <summary>
/// Gets the details of the form
/// </summary>
/// <param name=”FormName”></param>
public static SmartFormView LoadForm(string formname)
{

FormsManager frm = new FormsManager(“dlx”, 5555);
FormInfo forminfo = frm.GetForm(formname);
SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetViewDefinition(formname));

SmartFormView sf = new SmartFormView();
sf.guid = form.Guid;
sf.description = form.Description;

sf.displayname = form.DisplayName;
sf.name = form.Name;
sf.guid = form.Guid;
sf.theme = form.Theme;

return sf;
}
/// <summary>
/// Get a list of all the forms
/// </summary>
/// <returns></returns>
public static List<SmartFormView> GetAllForms()
{
List<SmartFormView> list = new List<SmartFormView>();
FormsManager frm = new FormsManager(“dlx”, 5555);
FormExplorer formexplorer = frm.GetForms();
foreach (SourceCode.Forms.Management.FormInfo forminfo in formexplorer.Forms)
{

SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(forminfo.Name));

list.Add(new SmartFormView
{
name = forminfo.Name,
displayname = forminfo.DisplayName,
description = forminfo.Description,
guid = forminfo.Guid,
version = forminfo.Version

});

}
return list;
}
/// <summary>
/// Get a list of all the forms that contain a certain view
/// </summary>
/// <param name=”ViewName”></param>
/// <returns></returns>
public static List<SmartFormView> GetAllFormsbyView(string ViewName)
{
List<SmartFormView> list = new List<SmartFormView>();
FormsManager frm = new FormsManager(“dlx”, 5555);
FormExplorer formexplorer = frm.GetFormsForView(ViewName);
foreach (SourceCode.Forms.Management.FormInfo forminfo in formexplorer.Forms)
{
SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetViewDefinition(forminfo.Name));

list.Add(new SmartFormView
{
name = forminfo.Name,
displayname = forminfo.DisplayName,
description = forminfo.Description,
guid = forminfo.Guid,
version = forminfo.Version

});

}
return list;
}
/// <summary>
/// List of form properties
/// </summary>
/// <param name=”FormName”></param>
/// <returns></returns>
public static List<SmartFormViewProperties> GetFormProperties(string FormName)
{
List<SmartFormView> list = new List<SmartFormView>();
FormsManager frm = new FormsManager(“dlx”, 5555);

SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));
Properties prop = new Properties();
return prop.ArtefactProperties( form.Properties);

}
/// <summary>
/// Gets a list of the form parameters
/// </summary>
/// <param name=”FormName”></param>
/// <returns></returns>
public static List<SmartFormViewParameters> FormParameters(string FormName)
{
List<SmartFormViewParameters> list = new List<SmartFormViewParameters>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));

foreach (SourceCode.Forms.Authoring.FormParameter parameter in form.Parameters)
{
list.Add(new SmartFormViewParameters
{
name = parameter.Name,
type = parameter.DataType.ToString(),
defaultvalue = parameter.DefaultValue
});
}

return list;

}

/// <summary>
/// Form Controls
/// </summary>
/// <param name=”FormName”></param>
/// <returns></returns>
public static List<SmartFormViewControls> FormControls(string FormName)
{
List<SmartFormViewControls> list = new List<SmartFormViewControls>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));

foreach (SourceCode.Forms.Authoring.Control control in form.Controls)
{
list.Add(new SmartFormViewControls
{
name = control.Name,
type = control.Type,
guid = control.Guid,

});

}
return list;
}
/// <summary>
/// Form Events
/// </summary>
/// <param name=”FormName”></param>
/// <returns></returns>
public static List<SmartFormViewEvents> FormEventsEvents(string FormName)
{
List<SmartFormViewEvents> list = new List<SmartFormViewEvents>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));

foreach (SourceCode.Forms.Authoring.Eventing.Event ev in form.Events)
{

if (ev.SourceType == SourceCode.Forms.Authoring.Eventing.EventSourceType.Rule)
{
list.Add(new SmartFormViewEvents
{
name = ev.Name,
type = ev.EventType.ToString(),
GUID = ev.Guid

});

}
}

return list;
}

/// <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;

}

/// <summary>
/// Conditions
/// </summary>
/// <param name=”FormName”></param>
/// <param name=”EventGUID”></param>
/// <param name=”HandleGUID”></param>
/// <returns></returns>
public static List<SmartFormViewConditions> ArtefactConditions(String FormName,Guid EventGUID,Guid HandleGUID)
{
List<SmartFormViewConditions> list = new List<SmartFormViewConditions>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));
var ev = form.Events[EventGUID].Handlers[HandleGUID];
SourceCode.Forms.Authoring.Eventing.Handler e = form.Events[EventGUID].Handlers[HandleGUID];

foreach (SourceCode.Forms.Authoring.Eventing.Condition condition in e.Conditions)
{

list.Add(new SmartFormViewConditions
{
GUID = condition.Guid,

});
}

return list;
}
/// <summary>
/// Actions
/// </summary>
/// <param name=”HandleGUID”></param>
/// <returns></returns>
public static List<SmartFormViewActions> ArtefactActionss(String FormName, Guid EventGUID, Guid HandleGUID)
{
List<SmartFormViewActions> list = new List<SmartFormViewActions>();
FormsManager frm = new FormsManager(“dlx”, 5555);

SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));
var ev = form.Events[EventGUID].Handlers[HandleGUID];
SourceCode.Forms.Authoring.Eventing.Handler e = form.Events[EventGUID].Handlers[HandleGUID];

foreach (SourceCode.Forms.Authoring.Eventing.Action action in e.Actions)
{
list.Add(new SmartFormViewActions
{
GUID = action.Guid,

viewguid = action.ViewGuid,
method = action.Method,
formguid = action.FormGuid,
executiontype = action.ExecutionType.ToString(),
controlguid = action.ControlGuid,
actiontype = action.ActionType.ToString()
});
}
return list;
}
/// <summary>
/// Actions Parameters
/// </summary>
/// <param name=”FormName”></param>
/// <param name=”EventGUID”></param>
/// <param name=”HandleGUID”></param>
/// <param name=”ActionGUID”></param>
/// <returns></returns>
public static List<SmartFormViewActionParameters> SmartFormViewActionParameters(String FormName, Guid EventGUID, Guid HandleGUID,Guid ActionGUID)
{
List<SmartFormViewActionParameters> list = new List<SmartFormViewActionParameters>();
FormsManager frm = new FormsManager(“dlx”, 5555);

SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));
SourceCode.Forms.Authoring.Eventing.Action e = form.Events[EventGUID].Handlers[HandleGUID].Actions[ActionGUID];

foreach (SourceCode.Forms.Authoring.Eventing.Mapping map in e.Parameters)
{

list.Add(new SmartFormViewActionParameters
{
targettype = map.TargetType.ToString(),
targetpath = map.TargetPath,
targetid = map.TargetID,
sourcevalue = map.SourceValue,
sourcetype = map.SourceType.ToString(),
sourcepath = map.SourcePath,
sourceid = map.SourceID

});
}

return list;
}
/// <summary>
/// Actions Results
/// </summary>
/// <param name=”FormName”></param>
/// <param name=”EventGUID”></param>
/// <param name=”HandleGUID”></param>
/// <param name=”ActionGUID”></param>
/// <returns></returns>
public static List<SmartFormViewActionParameters> SmartFormViewActionResults(String FormName, Guid EventGUID, Guid HandleGUID, Guid ActionGUID)
{
List<SmartFormViewActionParameters> list = new List<SmartFormViewActionParameters>();
FormsManager frm = new FormsManager(“dlx”, 5555);

SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));
SourceCode.Forms.Authoring.Eventing.Action e = form.Events[EventGUID].Handlers[HandleGUID].Actions[ActionGUID];

foreach (SourceCode.Forms.Authoring.Eventing.Mapping map in e.Results)
{

list.Add(new SmartFormViewActionParameters
{
targettype = map.TargetType.ToString(),
targetpath = map.TargetPath,
targetid = map.TargetID,
sourcevalue = map.SourceValue,
sourcetype = map.SourceType.ToString(),
sourcepath = map.SourcePath,
sourceid = map.SourceID

});
}

return list;
}
/// <summary>
/// Validation Messages
/// </summary>
/// <param name=”FormName”></param>
/// <param name=”EventGUID”></param>
/// <param name=”HandleGUID”></param>
/// <param name=”ActionGUID”></param>
/// <returns></returns>
public static List<SmartFormViewActionValidationMessage> SmartFormViewActionValidation(String FormName, Guid EventGUID, Guid HandleGUID, Guid ActionGUID)
{
List<SmartFormViewActionValidationMessage> list = new List<SmartFormViewActionValidationMessage>();
FormsManager frm = new FormsManager(“dlx”, 5555);

SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(FormName));
SourceCode.Forms.Authoring.Eventing.Action e = form.Events[EventGUID].Handlers[HandleGUID].Actions[ActionGUID];

foreach (SourceCode.Forms.Authoring.ValidationMessage val in e.Validation.Messages)
{

list.Add(new SmartFormViewActionValidationMessage
{
message = val.Message
});
}

return list;
}

}
}

View Object

6.Create a class called ‘View.cs’, like ‘Smartform.cs’ this class will contain all the methods for accessing information about views  on the K2 environment

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SourceCode.Forms.Management;
using SourceCode.Forms.Deployment;
using SourceCode.Forms;
using SourceCode.Forms.Authoring;

namespace CloudFish.FormSpider
{
public class Views
{

/// <summary>
/// Gets all the views on the environment
/// </summary>
/// <returns></returns>
public static List<SmartFormView> GetAllViews()
{
List<SmartFormView> list = new List<SmartFormView>();
FormsManager frm = new FormsManager(“dlx”, 5555);
ViewExplorer formexplorer = frm.GetViews();
foreach (SourceCode.Forms.Management.ViewInfo viewinfo in formexplorer.Views)
{

SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(viewinfo.Name));

list.Add(new SmartFormView
{
name = viewinfo.Name,
displayname = viewinfo.DisplayName,
description = viewinfo.Description,
guid = viewinfo.Guid,
version = viewinfo.Version,
});

}
return list;
}

/// <summary>
/// Gets all the views that are attached to a form
/// </summary>
/// <param name=”formname”></param>
/// <returns></returns>
public static List<SmartFormView> GetAllViews(string formname)
{
List<SmartFormView> list = new List<SmartFormView>();
FormsManager frm = new FormsManager(“dlx”, 5555);
ViewExplorer formexplorer = frm.GetViewsForForm(formname);
foreach (SourceCode.Forms.Management.ViewInfo viewinfo in formexplorer.Views)
{

SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(viewinfo.Name));

list.Add(new SmartFormView
{
name = viewinfo.Name,
displayname = viewinfo.DisplayName,
description = viewinfo.Description,
guid = viewinfo.Guid,
version = viewinfo.Version,

});

}
return list;
}
/// <summary>
/// List of view properties
/// </summary>
/// <param name=”ViewName”></param>
/// <returns></returns>
public static List<SmartFormViewProperties> GetViewProperties(string ViewName)
{
List<SmartFormView> list = new List<SmartFormView>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));
Properties prop = new Properties();
return prop.ArtefactProperties(view.Properties);

}
/// <summary>
/// Gets a list of the view parameters
/// </summary>
/// <param name=”ViewName”></param>
/// <returns></returns>
public static List<SmartFormViewParameters> ViewParameters(string ViewName)
{
List<SmartFormViewParameters> list = new List<SmartFormViewParameters>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));

foreach (SourceCode.Forms.Authoring.ViewParameter parameter in view.Parameters)
{
list.Add(new SmartFormViewParameters
{
name = parameter.Name,
type = parameter.DataType.ToString(),
defaultvalue = parameter.DefaultValue
});
}

return list;

}

/// <summary>
/// View Controls
/// </summary>
/// <param name=”ViewName”></param>
/// <returns></returns>
public static List<SmartFormViewControls> ViewControls(string ViewName)
{
List<SmartFormViewControls> list = new List<SmartFormViewControls>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));

foreach (SourceCode.Forms.Authoring.Control control in view.Controls)
{
list.Add(new SmartFormViewControls
{
name = control.Name,
type = control.Type,
guid = control.Guid,

});

}
return list;
}
/// <summary>
/// View Events
/// </summary>
/// <param name=”ViewName”></param>
/// <returns></returns>
public static List<SmartFormViewEvents> ViewEventsEvents(string ViewName)
{
List<SmartFormViewEvents> list = new List<SmartFormViewEvents>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));

foreach (SourceCode.Forms.Authoring.Eventing.Event ev in view.Events)
{
if (ev.SourceType == SourceCode.Forms.Authoring.Eventing.EventSourceType.Rule)
{
list.Add(new SmartFormViewEvents
{
name = ev.Name,
type = ev.EventType.ToString(),
GUID = ev.Guid

});

}
}

return list;
}

/// <summary>
/// Event Handlers
/// </summary>

/// <param name=”ViewName”></param>
/// <param name=”EventGUID”></param>
/// <returns></returns>
public static List<SmartFromViewHandlers> ViewHandlers(String ViewName, Guid EventGUID)
{
List<SmartFromViewHandlers> list = new List<SmartFromViewHandlers>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));
var ev = view.Events[EventGUID];

SourceCode.Forms.Authoring.Eventing.Event e = view.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;

}

/// <summary>
/// Conditions
/// </summary>
/// <param name=”ViewName”></param>
/// <param name=”EventGUID”></param>
/// <param name=”HandleGUID”></param>
/// <returns></returns>
public static List<SmartFormViewConditions> ArtefactConditions(String ViewName, Guid EventGUID, Guid HandleGUID)
{
List<SmartFormViewConditions> list = new List<SmartFormViewConditions>();
FormsManager frm = new FormsManager(“dlx”, 5555);
SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));
var ev = view.Events[EventGUID].Handlers[HandleGUID];
SourceCode.Forms.Authoring.Eventing.Handler e = view.Events[EventGUID].Handlers[HandleGUID];

foreach (SourceCode.Forms.Authoring.Eventing.Condition condition in e.Conditions)
{

list.Add(new SmartFormViewConditions
{
GUID = condition.Guid

});
}

return list;
}
/// <summary>
/// Actions
/// </summary>
/// <param name=”HandleGUID”></param>
/// <returns></returns>
public static List<SmartFormViewActions> ArtefactActionss(String ViewName, Guid EventGUID, Guid HandleGUID)
{
List<SmartFormViewActions> list = new List<SmartFormViewActions>();
FormsManager frm = new FormsManager(“dlx”, 5555);

SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));
var ev = view.Events[EventGUID].Handlers[HandleGUID];
SourceCode.Forms.Authoring.Eventing.Handler e = view.Events[EventGUID].Handlers[HandleGUID];

foreach (SourceCode.Forms.Authoring.Eventing.Action action in e.Actions)
{
list.Add(new SmartFormViewActions
{
GUID = action.Guid,

viewguid = action.ViewGuid,
method = action.Method,
formguid = action.FormGuid,
executiontype = action.ExecutionType.ToString(),
controlguid = action.ControlGuid,
actiontype = action.ActionType.ToString()
});
}
return list;
}
/// <summary>
/// Actions Parameters
/// </summary>
/// <param name=”ViewName”></param>
/// <param name=”EventGUID”></param>
/// <param name=”HandleGUID”></param>
/// <param name=”ActionGUID”></param>
/// <returns></returns>
public static List<SmartFormViewActionParameters> ViewActionParameters(String ViewName, Guid EventGUID, Guid HandleGUID, Guid ActionGUID)
{
List<SmartFormViewActionParameters> list = new List<SmartFormViewActionParameters>();
FormsManager frm = new FormsManager(“dlx”, 5555);

SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));
SourceCode.Forms.Authoring.Eventing.Action e = view.Events[EventGUID].Handlers[HandleGUID].Actions[ActionGUID];

foreach (SourceCode.Forms.Authoring.Eventing.Mapping map in e.Parameters)
{

list.Add(new SmartFormViewActionParameters
{
targettype = map.TargetType.ToString(),
targetpath = map.TargetPath,
targetid = map.TargetID,
sourcevalue = map.SourceValue,
sourcetype = map.SourceType.ToString(),
sourcepath = map.SourcePath,
sourceid = map.SourceID

});
}

return list;
}
/// <summary>
/// Actions Results
/// </summary>
/// <param name=”ViewName”></param>
/// <param name=”EventGUID”></param>
/// <param name=”HandleGUID”></param>
/// <param name=”ActionGUID”></param>
/// <returns></returns>
public static List<SmartFormViewActionParameters> ViewActionResults(String ViewName, Guid EventGUID, Guid HandleGUID, Guid ActionGUID)
{
List<SmartFormViewActionParameters> list = new List<SmartFormViewActionParameters>();
FormsManager frm = new FormsManager(“dlx”, 5555);

SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));
SourceCode.Forms.Authoring.Eventing.Action e = view.Events[EventGUID].Handlers[HandleGUID].Actions[ActionGUID];

foreach (SourceCode.Forms.Authoring.Eventing.Mapping map in e.Results)
{

list.Add(new SmartFormViewActionParameters
{
targettype = map.TargetType.ToString(),
targetpath = map.TargetPath,
targetid = map.TargetID,
sourcevalue = map.SourceValue,
sourcetype = map.SourceType.ToString(),
sourcepath = map.SourcePath,
sourceid = map.SourceID

});
}

return list;
}
/// <summary>
/// Validation Messages
/// </summary>
/// <param name=”ViewName”></param>
/// <param name=”EventGUID”></param>
/// <param name=”HandleGUID”></param>
/// <param name=”ActionGUID”></param>
/// <returns></returns>
public static List<SmartFormViewActionValidationMessage> ViewActionValidation(String ViewName, Guid EventGUID, Guid HandleGUID, Guid ActionGUID)
{
List<SmartFormViewActionValidationMessage> list = new List<SmartFormViewActionValidationMessage>();
FormsManager frm = new FormsManager(“dlx”, 5555);

SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(ViewName));
SourceCode.Forms.Authoring.Eventing.Action e = view.Events[EventGUID].Handlers[HandleGUID].Actions[ActionGUID];

foreach (SourceCode.Forms.Authoring.ValidationMessage val in e.Validation.Messages)
{

list.Add(new SmartFormViewActionValidationMessage
{
message = val.Message

});
}

return list;
}

}
}

7.Now we can build the solution and we are ready to tell K2 about it and make it a service object and then a SmartObject.

You can download the code from this location

 

Creating the Service Object

Creating the service object is really easy, open up the ‘SmartObject Service Tester’

ServiceObjectTester

1.Right Click on the ‘Endpoints Assembly’ and click on ‘Register Service Instance’

AddServiceInstance

2.In the ‘Assembly Full Path’ enter in the full address of where the dll of the assembly is kept.

3.Click on ‘Next’ and enter in ‘Form Spider’ in the ‘Display Name’ and ‘System Name’

4.Click ‘Finish’

K2 will then integrate the Dll and map out all the public static methods.

ServiceObjectTester

Now that we have the service objects we can now create the SmartObjects. For this project i have just created two SmartObjects one for Smartforms and for the Views. Each of these objects will contain all the public static methods we created in the class library earlier.

Below is an example of one

ServiceObjectTester

 

 

 

More information on Endpoint Assemblies can be found here 

 

In the final part of this series we will take the SmartObjects and build the  Smartforms and Views.

 

 

 

 

Building a K2 Smartform spider part 4

Rules

In part 3, we looked at how to get additional information from Smartform such as parameters and controls. In part 4 we will now look at how to be able to get the rules from the forms and views.

This is probably most complex part of the API as it involves a number of different methods to get the  necessary information.

To access the rules  of the artifact we need to use the following code as we did in part 3 this will give us access to the Events object.

Code for a Smartform

SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(‘form name’));

Code for a view

SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(‘View Name’));

So lets take a look and see what we can from the events object

Events

Majority of rules in Smartforms and views start with an event.  An event is when something happens such as a ‘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;
}

The above method gives a list of all the events on the artifact and access to more additional objects

First thing we do is we loop through the event collection and we check to see if the event type is the ‘Rule’ type.

if (ev.SourceType == SourceCode.Forms.Authoring.Eventing.EventSourceType.Rule)

The objects we want to have access to are the following

  1. Name
  2. Type
  3. Handlers
  4. Properties

The important object from this is the Handlers, as this will give us access to the Conditions and Actions of the rule.

Handlers

The handler object is where we can drill down into the rule and access the conditions of the rule and also the actions of the rule.

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;

}

The methods starts by passing in the handler object from the previous method, now the method can loop through the all the handlers for that particular event

foreach (SourceCode.Forms.Authoring.Eventing.Handler handle in handlers)

On each loop, we can then access the following objects

  1. Name
  2. Conditions
  3. Actions

Conditions

Conditions check to see if something is equal to true, for example ‘all the required fields are entered’.

public List<SmartFormViewConditions> ArtefactConditions(SourceCode.Forms.Authoring.Eventing.ConditionCollection conditions)
{
List<SmartFormViewConditions> list = new List<SmartFormViewConditions>();
foreach (SourceCode.Forms.Authoring.Eventing.Condition condition in conditions)
{

list.Add(new SmartFormViewConditions
{
Property = condition.Properties
});
}

return list;
}

 

Actions

So we can access the event, the conditions and now lets look how we can access the actions of a rule.

public List<SmartFormViewActions> ArtefactActionss(SourceCode.Forms.Authoring.Eventing.ActionCollection actions)
{
List<SmartFormViewActions> list = new List<SmartFormViewActions>();

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()
});
}
return list;
}

Using the above method, we can get the complete information of the action.  The action opens up some additional objects that we can explore to get more detailed information about the action.

Below are some examples of the more useful ones

Action Parameters

public List<SmartFormViewActionParameters> SmartFormViewActionParameters(SourceCode.Forms.Authoring.Eventing.MappingCollection parameters)
{
List<SmartFormViewActionParameters> list = new List<SmartFormViewActionParameters>();
foreach (SourceCode.Forms.Authoring.Eventing.Mapping map in parameters)
{

list.Add(new SmartFormViewActionParameters
{

validation = map.Validation,
targettype = map.TargetType.ToString(),
targetpath = map.TargetPath,
targetid = map.TargetID,
sourcevalue = map.SourceValue,
sourcetype = map.SourceType.ToString(),
sourcepath = map.SourcePath,
sourceid = map.SourceID

});
}

return list;
}

Action Results

public List<SmartFormViewActionParameters> SmartFormViewActionResults(SourceCode.Forms.Authoring.Eventing.MappingCollection Results)
{
List<SmartFormViewActionParameters> list = new List<SmartFormViewActionParameters>();
foreach (SourceCode.Forms.Authoring.Eventing.Mapping result in Results)
{

list.Add(new SmartFormViewActionParameters
{

validation = result.Validation,
targettype = result.TargetType.ToString(),
targetpath = result.TargetPath,
targetid = result.TargetID,
sourcevalue = result.SourceValue,
sourcetype = result.SourceType.ToString(),
sourcepath = result.SourcePath,
sourceid = result.SourceID

});
}
return list;
}

Action Validation

public List<SmartFormViewActionValidation> SmartFormViewActionValidations(SourceCode.Forms.Authoring.ValidationResult validations)
{

List<SmartFormViewActionValidation> list = new List<SmartFormViewActionValidation>();

SourceCode.Forms.Authoring.ValidationResult validation = validations;

list.Add(new SmartFormViewActionValidation
{
messages = validation.Messages,
status = validation.Status.ToString()
});
return list;
}

Action Messages

public List<SmartFormViewActionValidationMessage> SmartFormViewActionValidationMessages(ValidationMessageCollection messages)
{
List<SmartFormViewActionValidationMessage> list = new List<SmartFormViewActionValidationMessage>();
foreach (ValidationMessage message in messages)
{
list.Add(new SmartFormViewActionValidationMessage
{
message = message.Message
});
}
return list;
}

 

In the next part, we will take the methods we have explored in parts 1 to 4 and put it altogether to build the form spider.

I will make the source code available at the end of this series.

 

Building a K2 Smartform spider part 3

In part 1 we looked at how to get a list of all the Smartforms and views on the K2 server and in part 2 we looked how we could filter those lists by finding all the forms that particular view has been used in etc..

In part 3 we will look at how to go deeper into those artifacts with out actually having to open up the artifact in edit mode to see whats go on.

Getting started

To get started we need some additional objects that are not available in the standard properties that is found in part 1. We need to get  access to the definition of the artifact (basically the XML).

Some of the objects that we will be able to access are

  1.  Properties
  2. Controls
  3. Parameters
  4. Events

To access these additional objects we need to use the following line of code.

Code for a Smartform

SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(‘form name’));

Code for a view

SourceCode.Forms.Authoring.View view = new SourceCode.Forms.Authoring.View(frm.GetViewDefinition(‘View Name’));

Parameters

So lets start with an easy one and get the list of parameters that are available on that particular view.

Getting a list of parameters for a form

The below method gets a list of all the parameters for 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 has the input parameter of type ‘parameter collection’ which we can using the code the code from the ‘Getting Started’ section.

SourceCode.Forms.Authoring.FormParameter parameter in parameters

We then loop through the all the parameters in the collection, where we can get  a list of the  following parameter properties

  1. Name
  2. Type
  3. Default value

Example of executing the method

SourceCode.Forms.Authoring.Form form = new SourceCode.Forms.Authoring.Form(frm.GetFormDefinition(forminfo.Name));

ArtifactParameters(form.parameters);

 

Controls

We had a look at getting the parameters so how about getting a list of all the controls

Like the parameters we pass in the ‘control’ object from the artifact and then we loop through all the controls in the collection.

The properties that we will be  using are the

  1. Name
  2. Type
  3. GUID
  4. Properties (properties of the control)

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 is universal for both Smartforms and views.

This brings us nicely to the properties object

Properties

The below property method can be used to get the properties of a control such as it’s width, field etc.. But also more properties based from a view or Smartform and even a  rule.

public List<SmartFormViewProperties> ArtefactProperties(SourceCode.Forms.Authoring.PropertyCollection properties)
{
List<SmartFormViewProperties> list = new List<SmartFormViewProperties>();
foreach (SourceCode.Forms.Authoring.Property prop in properties)
{
list.Add(new SmartFormViewProperties
{
name = prop.Name,
value = prop.Value,

});

}
return list;
}

The method accepts the type ‘Property collection’ as an input parameter and then loops through the list of properties.

  1. Name is the name of the property  for example ‘Width’
  2. Value is value of the property for example ‘200px’

 

In the next part we will look at the ‘Events’ as there is a lot that goes into this.

 

I will make the source code available at the end of this series.

 

 

 

Building a K2 Smartform spider part 2

Taking part 1 further

So in part 1 we looked at a couple of basic methods for getting a list of  Smartforms and Views that are currently on the K2 Server. Which can be really handy if you wanted to build a library of all your forms and views.

But can take this further? We can already from the K2 designer expand the form and see what views make up that form, but we can’t though click on a view and see what forms it is being used in.

Finding all forms for a selected view

So in this section I am going to go through how we can retrieve a list of all the forms that a view has been used in. It pretty much uses the same code as the previous examples except this time we will be using  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;
}

 

So you can see from the code above , its practically the same as just looping through all the forms. The only difference is this method call

frm.GetFormsForView(viewName)

All we do is call the above method and pass in the name of the view and we can then loop through all the forms  where that view is being used.

Finding all views for a selected form

So as we have done this for the view, it would only be fair if we did this for the form and like the example above it is very similar to the examples in part 1.

public List<SmartFormView> GetAllViews(string formname)
{
List<SmartFormView> list = new List<SmartFormView>();
FormsManager frm = new FormsManager(servername, port);
ViewExplorer formexplorer = frm.GetViewsForForm(formname);
foreach (SourceCode.Forms.Management.ViewInfo viewinfo in formexplorer.Views)
{

list.Add(new SmartFormView
{
name = viewinfo.Name,
displayname = viewinfo.DisplayName,
description = viewinfo.Description,
guid = viewinfo.Guid,
version = viewinfo.Version,

});

}
return list;

}

If we look at the code above the only difference is the method that is being called

frm.GetViewsForForm(formname)

We call this method and pass in the name of the form, or even the GUID and it then returns a list of all the views that are in the form.

Other methods

We can take this even further  and we can get a list of views that is being used by  a SmartObject and even a  list of views/forms that is calling a specific workflow

GetViewsForObject(Smartobject GUID)

GetViewsForProcess(Process Name)

GetFormsForProcess(Process Name)

 

In part 3 we will take these a bit further and drill down into the views and forms to see what makes up those artifacts.

I will make the source code available at the end of this series.

Building a K2 Smartform spider part 1

Introduction

For the last 6 months, I have been working on a really big project using K2 probably one of the biggest that has happened in the UK. With multiple different teams working on the same environment,collaborating to build a solution for the client.

One of the things that struck me, that i noticed was missing from the K2 platform was the ability to see what artifacts (forms, views, SmartObjects and workflows) made up a solution. Yes i could go into edit mode of each form, view etc.. and  see what components were being used, but can take  up a lot of time. What I wanted was a way to click onto a view and see what Smartforms the view was being used in etc..

So this got me thinking about what could I do, that wouldn’t involve too much hard work. Well 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 / inspector and their was some off the shelf options where i could navigate through the forms etc.. But again that wasn’t really what I wanted, I didn’t really want to pay for a tool and if I could see this information inside of a smartform that would then be even better. So I started looking at the SmartForm API that is provided out of the box with K2 Smartforms.

 

The API

It may not be brilliantly documented, but with a little bit of research and messing around there is a lot you can do with this API. From exploring the contents of a view or form to being able to dynamically add parameters and controls into your artefact.


So what assemblies do I need?

The assemblies that I will be using in the following example are

  1. SourceCode.Forms
  2. SourceCode.Forms.Management
  3. SourceCode.Forms.Authoring

 

 

Hello World

Ok, so lets start off with a basic example of using this API. 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;
}

 

So in the code above  we create an instance of  an object that is going to contain all the details for each form.

List<SmartFormView> list = new List<SmartFormView>();

We can now tell the API what K2 server to look and make a connection to that server

FormsManager frm = new FormsManager(servername, port);

 

Now we have established a connection, we can now call this method to a get a list of all the forms available on the K2 server.

frm.GetForms();

 

All we now is too loop through the list of the forms and for each form get all the properties of the form, such as the form name, description, version etc..

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()

});

}

 

 

Getting  a list of all the  views

We can also do the same with the views and get a list of all the views on the server

public List<SmartFormView> GetAllViews()
{
List<SmartFormView> list = new List<SmartFormView>();
FormsManager frm = new FormsManager(servername, port);
ViewExplorer formexplorer = frm.GetViews();
foreach (SourceCode.Forms.Management.ViewInfo viewinfo in formexplorer.Views)
{

 

list.Add(new SmartFormView
{
name = viewinfo.Name,
displayname = viewinfo.DisplayName,
description = viewinfo.Description,
guid = viewinfo.Guid,
version = viewinfo.Version,

});

}
return list;
}

As you can see they are very similar the main difference is that view method uses ‘View’ version of the explorer object and not the ‘form’ version like the Smartform version.

In the next part we will take this a step further and have a look at searching for specific forms and views.

I will make the source code available at the end of this series.