Creating K2 4.5 Custom Inline Functions

The ability to create your own Inline Functions is one of the best customisation features of 4.5 and it is so quick and easy to use.  You can either use the available K2 Visual Studio Custom Inline Function template or create it from scratch.   This post assumes that you will be using the template as it is quicker and easier to get started. (Custom Inline Function Template – right click and select ‘Save Target As’)

  1. I have attached the template to this post with a ‘.doc’ extension, just download it and rename the extension to ‘.zip’ and copy the zip file (without extracting it) to the following folder (your path may differ slightly):
    C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\K2
  2. Launch Visual Studio and create a new project, make sure to select the project template from the K2 section.
  3. I called my project and hence the namespace TestInline
  4. Note that the following .NET references are added to the project:
    System.Drawing
    SourceCode.Framework
    SourceCode.Workflow.Functions.Design
  5. Note the Using statements in the code for the class as follows:
         using SourceCode.Framework;
         using SourceCode.Framework.Design;
         using System.Drawing.Design;
  6. The template uses a resource file for holding names for the function, category etc.  Double click on the Resource file (Resources.resx) in the Solution Explorer frame (in the properties folder) and edit the values as follows:
         Name: Function1Description Value: This is test Inline Function Comment: Description for the function
         Name: Function1Name Value: Test Function 1 Comment: Name of the custom inline function
         Name: Functions1CategoryName Value: My Functions Comment: The category to hold the function
         Name: SourceValueDescription Value: Test Input Field Comment: A description for the first (and only) parameter
         Name: SourceValueNameValue: TestInput Comment: The name for the first (and only) parameter
    Note that you can’t easily store your custom inline functions within existing categories so you will need to define your own category heading.
    You can have multiple parameters, the above example holds only one.
    You can call the resource fields whatever you want.
  7. You can add a different icon for your function but for now we’ll go with the default one provided by the template. 
  8. Back in the code, above the class edit the Category directive to descibe
         [Category(“TestInline.Properties.Resources”, “Functions1CategoryName”, typeof(TestInlineFunction))]
         The first parameter is the fully qualified name of the resource file.
         The second parameter is the name of the resource string that holds the name of the category that you want to use for your function.
         The third parameter is the name of the public class that holds the code for your function.
  9. You can then add the code for the function which can be as simple or as complex as you like.  My simple example just accepts one text input and then returns the same value with a “Hello World!” prefix.  Nothing groundbreaking I’m afraid.  So start with defining your function – as with the class, there are some properties to add which tell K2 about your function.
            [DisplayName(“TestInline.Properties.Resources”, “Function1Name”, typeof(TestInlineFunction))]
            [Description(“TestInline.Properties.Resources”, “Function1Description”, typeof(TestInlineFunction))]
            [K2Icon(typeof(TestInlineFunction), “Resources.Icon.png”)]
            // Must be a public static method.
    The first line refers to the name of the function as defined in the resource file.
    The second line refers to a description of the function as defined in the resource file.
    The third line is a pointer to the icon in the resource file.
  10. The next step is to define your properties and functionality.  My example just has one property and returns a string.
         public static string Function1(
         [DisplayName(“TestInline.Properties.Resources”, “SourceValueName”, typeof(TestInlineFunction))]
         [Description(“TestInline.Properties.Resources”, “SourceValueDescription”, typeof(TestInlineFunction))]
         string InputString)
          {
         // Return the result.
              return “Hello World! ” + InputString;
            }
    The first line is the uninspired name of my only function – Function1
    The second line refers to the name of the property as defined in the resource file.
    The third line refers to a description of the property as defined in the resource file.
    The fourth line defines the input parameter to the function as a string.
    The rest just does the concatenation and returns the result
  11. That’s it.  You then activate it and it’s available.  Look out for my next post which will discuss activation.

FULL SOURCE CODE

using System;
using System.Collections.Generic;
using System.Drawing.Design;
using System.Text;

using SourceCode.Framework;
using SourceCode.Framework.Design;

namespace TestInline
{
    /// <summary>
    /// A class comprising functions.
    /// </summary>
    // Must be a public class.
    [Category(“TestInline.Properties.Resources”, “Functions1CategoryName”, typeof(TestInlineFunction))]
    public class TestInlineFunction
    {

        #region Default Constructor
        /// <summary>
        /// Instantiates a new Notification1.
        /// </summary>
        public TestInlineFunction()
        {
            // No implementation necessary.
        }
        #endregion

        #region Methods

        #region string InlineFunction(string sourceValue)
        /// <summary>
        /// Performs a function.
        /// </summary>
        /// <param name=”sourceValue”>A string representing the source value for which the function will perform an operation. Does not have to be string.</param>
        /// <returns>A string representing the result of the function. Does not have to be string.</returns>
        [DisplayName(“TestInline.Properties.Resources”, “Function1Name”, typeof(TestInlineFunction))]
        [Description(“TestInline.Properties.Resources”, “Function1Description”, typeof(TestInlineFunction))]
        [K2Icon(typeof(TestInlineFunction), “Resources.Icon.png”)]
        // Must be a public static method.
        public static string InlineFunction(
            [DisplayName(“TestInline.Properties.Resources”, “SourceValueName”, typeof(TestInlineFunction))]
            [Description(“TestInline.Properties.Resources”, “SourceValueDescription”, typeof(TestInlineFunction))]
            string sourceValue)
        {
            // Return the result.
            return “Hello World! ” + sourceValue;
        }
        #endregion

        #endregion
    }
}

Advertisements

About rearcardoor

Chairman and founder of ImageFast Ltd, a leading UK ECM consultancy business and Microsoft Gold Partner. Over 20 years experience delivering successful ECM projects utilising scanning, data capture, document management, records management, workflow, BPM and SharePoint.
This entry was posted in K2 and tagged . Bookmark the permalink.

2 Responses to Creating K2 4.5 Custom Inline Functions

  1. MBlair says:

    Is there anywhere I can download pre-built inline functions? I’m particularly interested in anything that can connect to AD… thanks
    mb

  2. rearcardoor says:

    Thanks for the quick post! K2 Underground is your best bet as there is so much useful stuff posted on there. Re AD, there is one that I know of by Tim Knechtel at:
    http://www.k2underground.com/groups/caprivi_active_directory_inline_functions/blog/archive/2010/05/06/caprivi-active-directory-inline-function.aspx

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