Skip to main content

In this second post to the series, Function to Microservice with the Nintex Workflow Platform, I look at how you can add your function to Nintex Workflow 2013 as an inline function.

 

In these posts I reference a console application containing a function that converts a XML to JSON, or JSON to XML. The function will end up in a workflow that takes a string of JSON from a SharePoint list, passes it through a String Builder Action that contains the inline function, storing the result in a workflow variable, and then storing the result in in the list.

 

Table of Contents

Table of contents

 

Inline Function

 

In Nintex Workflow 2013, an inline function, also known as a string function, is a .NET Framework static method that can be used like any other workflow reference in workflow actions. Nintex Workflow 2013 parses, evaluates, and resolves the reference by invoking the method and replacing the reference with the value returned from that method.

 

After developing your function in a custom class that contains the static methods you would like make available in the Insert References window, you can deploy the DLL to your SharePoint environment, and then use the Nintex Workflow Administration tool to register the namespace and method containing the function along with the usage syntax and description.

 

A SharePoint user can then access the function in the Insert Reference button in the Nintex Workflow Designer, reference the  useage syntax and description, and then insert the inline function into a text box in their workflow. For example, I added the JsonConverter as a custom inline function, and is listed in the InLine Functions tab in the Insert References window in the Workflow Designer. For more information about Inline Function from the user of the Workflow Desinger, refer to the User Assistance topic for Inline Functions. With our function it would look a bit like this in the Inline Functions tab.

 

An inline function containing the example function used in this series of blog posts.

 

Set Up your Inline Function Project

You will need to have a SharePoint Development environment with the following installed:

  • Office and SharePoint SDK for Visual Studio
  • Nintex Workflow SDK 2013 installed in Visual Studio
  • Nintex Workflow 2013
  • SharePoint 2013

For more information on the requirements for the Nintex Workflow 2013 SDK, see Installing and Using the Nintex Workflow 2013 SDK.

Steps to Create your Inline Function

1. Create an empty SharePoint 2013 Solution in Visual Studio.

2. Write your function as a method in a class. You can use the same class to contain your library inline functions. In the following example I have supplied two files containing two methods (that is, functions): a handy string-to-Pig-latin converter method (Piglatin(string)) for your Pig Latin needs, and the JsonConvert method, JsonConvert(string)) that Instantiate the JSONConverterAction class.

Custom.cs

namespace Custom.InlineFunctions.Custom{        /// <summary>        /// Custom class. Is used to expose static methods which can be used for insert references.        /// </summary>        public class Custom        {        /// <summary>        /// Converts a string into Pig Latin.        /// </summary>        /// <param name="piggy">The string to translate to Pig Latin.</param>        /// <returns>Returns the string in Pig Latin.</returns>        public static string Piglatin(string piggy)            {                char firstchar = piggy 0];                string isVowel = "aeiouAEIOU";                if (isVowel.IndexOf(firstchar) >= 0)                {                    string output = piggy + "yay";                    return output;                }                else                {                    string stem = piggy.Substring(1);                    string output = stem + firstchar + "ay";                    return output;                }             }        /// <summary>        /// A console application that converst from XML to JSON, or form JSON to XML.        /// </summary>        /// <param name="inputtext">Text as either JSON or XML.</param>        public static string Jsonconvert(string inconvert)        {                      try            {                string inputtext = inconvert;                JsonConverterAction converter = new JsonConverterAction();                string outconvert = converter.ConvertIt(inputtext);                return outconvert;             }            catch            {                return "Not able to convert the string.";                    }        }    }    }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

JSONConverterAction.cs

using System;using System.IO;using System.Xml;using System.Xml.Linq;using Newtonsoft.Json;/// <summary>/// Converts text form JSON to XML or from XML to JSON. The converter detects the first character in the string and then converst to XML or JSON./// <parameter>Input string (in either JSON or XML format.</parameter>/// </summary>namespace Custom.InlineFunctions.Custom{    internal class JsonConverterAction    {        public string ConvertIt(string inputText)        {            var convertedtext = inputText.Trim();            var firstChar = inputText<0].ToString();            switch (firstChar)            {                case "1":                case "<":                    convertedtext = XmlToJson(convertedtext);                    break;                case "2":                case "{":                    convertedtext = JsonToXml(convertedtext);                    break;                case "3":                case "o":                    convertedtext = JsonToXml(convertedtext);                    break;                default:                    convertedtext = JsonToXml(convertedtext);                    break;            }            return convertedtext;        }        //Converts a JSON string to XML.        public string XmlToJson(string inputXMLasString)        {            try            {                string outputJsonAsString = "";                XmlDocument xmldoc = new XmlDocument();                xmldoc.LoadXml(inputXMLasString);                outputJsonAsString = JsonConvert.SerializeXmlNode(xmldoc);                return outputJsonAsString;            }            catch (Exception ex)            {                string expectmessage = ex.ToString();                string returnmessage = "XML not valid. " + expectmessage;                return returnmessage;            }        }        //Converts an XML string to JSON.        public string JsonToXml(string inputJSONasString)        {            try            {                string outputXmlAsString = "";                XNode node = JsonConvert.DeserializeXNode(inputJSONasString, "Root");                StringWriter stringWriter = new StringWriter();                XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);                node.WriteTo(xmlTextWriter);                outputXmlAsString = stringWriter.ToString();                return outputXmlAsString;            }            catch (Exception ex)            {                string expectmessage = ex.ToString();                string returnmessage = "JSON not valid. " + expectmessage;                return returnmessage;            }        }    }}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

3. Build and Deploy the WSP to SharePoint from your project.

4. Register the Inline Function with Nintex Workflow using the NWAdmin tool from your SharePoint environment. You can find an explanation of the addinline function command in the reference. You will need the fully qualified assembly name in addition to a name for your function, the namespace, class, and static method name.

The entry should look something like this:

NWAdmin.exe -o AddInlineFunction -functionalias  fn-Jsonconvert -assembly "Custom.InlineFunctions.Custom, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1b923b394396c09b" -namespace Custom.InlineFunctions.Custom -typename Custom -method Jsonconvert -usage "fn-Jsonconvert(string)" -description "Returns JSON as XML or XML as JSON. Note, cannot convert a list of JSON objects as XML."‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Or in the SharePoint 2013 Management shell:

This blog post is a quick overview of the steps. You can find more detailed explanations in the Nintex Workflow and Forms 2013 SDK documentation. 

Steps to Use the Inline Function

1. Create a SharePoint list where you can place the input to the workflow and the output processed by the inline function.

2. Click LIST, and then select Create Workflows with Nintex Workflows.

3. Create a workflow two action, the Builder String Action and the Set field value action.

4. Click the Build String action, select Configure, and then click Insert Reference. Click Insert Reference, and then find fn-Jsonconvert converter in the list, and add to the the window,  fn-Jsonconvert(). And then between the parenthesis, add a variable reference to the In column in the SharePoint list. by clicking  fn-Jsonconvert, clicking the Item Properties tab, and selecting the name of the column.

 

5. Select the workflow variable to store the output of the function.

6. Configure the Set Field value action to store the workflow variable in the output column.

7. Save and publish your workflow.

You can type JSON or XML in the In column, run the workflow, and the conversion will appear in the Out column.

Be the first to reply!

Reply