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.

5 thoughts on “Building a K2 Smartform spider part 1

  1. Hi. Thanks for a great blog. I have tried out your code but I get a compile error.
    “The type or namespace name ‘SmartFormView’ could not be found (are you missing a using directive or an assembly reference?”
    I have added the 3 assemblies as you describe. Do you have any clue?

    Like

    1. Hi, yeah ‘SmartFormView’ is just a custom class to contain the data. See example below. I will be releasing the complete source code at the end of this series.

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

      }

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s