Server Code Event – K2 BlackPearl

One of the best aspects of K2 BlackPearl is the ability to include CODE – sometimes in response to an “event” of some kind – such as before or after an event or activity :

image

This can be either “standard C#” code – or a Windows Workflow designer “chunk”.

The example I’ll walk through is more with regard to “standard C#” code.  

Or – more specifically – using a “Default Server Event (Code)”.

Some background – and the reason behind this post.  

We have a workflow with an InfoPath form, and need the K2 process to update/inject values into the XML behind the form.  

There used to be a specific “event wizard” for this with K2.NET 2003 – but no longer, and have to use some code.  

The other aspect being covered is “re-using / re-factoring” the code into a shared module.   And calling this from within K2 – ie. an external DLL.

OK – so here’s what I’ve baked already :

  • InfoPath form – with two views – one for Submit and another for Manager
  • Very (very) basic workflow – initiated from a SharePoint forms library
  • InfoPath client event to approve/decline
  • Send an email to notify if approved/declined

image

If you’re lost already – I suggest you don’t read any further, there’s a great book that will help with some learning’s, and getting up-to-speed with K2.

Here’s what the user’s see – with the form initiated from clicking “NEW” in the document library :

image

And the manager view – with an additional field to update some information (synopsis) – not a great example, but it’ll prove the point.

image

This runs fine – but what we need to do is update the XML in the InfoPath form BEFORE it’s shown to the manager – with the “synopsis” field pre-populated – ideally using other data in the form – but just hard-coded for now.

This will be reviewed by the manager – and updated as necessary – and used in the body of the email to be sent out.

Here’s the basic steps that I’m taking to do this :

  • Within the K2 BlackPearl “solution”, add another “project” as with any other web application within Visual Studio.
  • Choose “Visual C#” – and then “Class Library”
  • Name the new class project “K2Helper”
  • Rename the class library (CS) to be “InfoPathHelper”

The solution (and projects) should NOW look something like this (click to expand/enlarge):

image

We’re going to add a method that can then be called from within the K2 project.  This will take some parameters relating to “InfoPath XML” – which will become apparent soon enough.

  • 1st step is to make sure the “using System.Xml” has been added at the top of the class
  • And then – add a method :

public static string UpdateInfoPathField(string xml, string nameSpace, string fieldName, string fieldValue)
{

}

The next set of code has been sourced / stolen / copied from my good buddy Jason Apergis – check out this post – related to “updating InfoPath fields” :

Working with InfoPath XML and Archiving it with BlackPearl and SharePoint

//Load up the XML from input parameter
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

//Create a name space manager for InfoPath
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("my", doc.DocumentElement.GetNamespaceOfPrefix("my"));

//Now set the value for XPath
string xPath = "//my:" + nameSpace + "/my:" + fieldName;

//update the value in the XML
doc.SelectSingleNode(xPath, nsMgr).InnerText = fieldValue;

//return the "fixed" xml back as a string
return doc.OuterXml;

This is the same code – shown in the nice Visual Studio editor :

image

OK – that’s about it.  

Do a quick build of the project (just the “K2Helper” project) – and verify that all is OK.   

Now – we can turn our attention to the K2 workflow project.

  • Double-click on the KPRX file – to display the K2 workflow
  • RIght click somewhere in the white space – and select properties
  • Need to add a reference to the DLL of the project (above)

image

  • Click Add
  • Then, click Browse
  • Navigate to the BIN folder, and select the DLL – and click OK

image

Now that there is a reference to the project/DLL, we can include a reference within code – and use the method as needed.

  • Drag a “Default Server Event (Code)” onto the design pane
  • Position this ABOVE the Manager Approval step
  • Move the lines & boxes around – and join it up
  • Should look something like this :

image

Now, we can actually add the code :

  • Right-click on the “lightning bolt”

image

  • Click View Code > Event Item
  • Add the statement at the top “using K2Helper”
  • The following code then :
      • Grabs the XML
      • Passes the XPath, FieldName and Value to set
      • Set the xml BACK to the K2 XMLField – and thus the form

//Get the XML Field string
string xml = K2.ProcessInstance.XmlFields[0].Value.ToString();

//pass it to the class, and return the updated XML to the string value
xml = InfoPathHelper.UpdateInfoPathField(xml, "meetingAgenda/my:meeting", "meetingSynopsis", "Lunch will be provided");

//Now set the value back into the XML Field
K2.ProcessInstance.XmlFields[0].Value = xml;

This is the same code – shown in the nice Visual Studio editor :

image

And so, when the manager approval step is shown, there is a value that’s (magically) appeared :

image

*** Obviously – this can be extended – to grab out the fields within the form, and compile the “synopsis” of the meeting. 

Eg,   “Joe Barnes will chair the meeting to discuss Development KickOff.  Please be prompt, as the meeting will commence at 11:00 – make your way to Rm 402

The manager could then update as needed – and the TEXT would be sent in the email in the following step.

That’s about all it takes – in case there are any questions !!?!    *smile*

Here’s what was shown :

  • Default Server Event (Code) – calling to an external DLL
  • Method to update InfoPath values (thx Jason A)

Hopefully that quick overview is of some use.  

Pls. leave a comment if you would like to discuss – or to try out some more examples (if you’re NICE, I might even email you the code/project)

🙂

Advertisements
This entry was posted in K2 blackpearl. Bookmark the permalink.

4 Responses to Server Code Event – K2 BlackPearl

  1. Damion says:

    Just dropping by.Btw, you website have great content!

    ______________________________
    THIS IS A DREAM JOB! No gimmicks. No catch. Work from home whenever you want!

  2. Francine says:

    I was able to create the DLL and add it in my Blackpoint project. I could not figured out how to setup the different variables in the Reference event in BlackPoint. How do we use this DLL in blackpoint reference even?

  3. Thanks for your great post!

    Just one question: I gues this is creating a code activity, so what will happen when you upgrade to a higher .NET framework version (e.g. 4.0 where the whole WF ist redesigned)?

  4. Precisely how much time did it require u to
    write “Server Code Event – K2 BlackPearl devK2.
    net”? It also has a lot of decent knowledge. Thanks ,
    Claudia

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