Blog Post

SharePoint Workflow Power Pack User Guide — Creating Custom Methods

Clavin Fernandes
Illustration: SharePoint Workflow Power Pack User Guide — Creating Custom Methods

In part 4 of our series of User Guide related blog postings for the Muhimbi Workflow Power Pack for SharePoint we show how to create your own methods in a WPP script in order to keep the code organised and easy to maintain.

A quick introduction In case you are not familiar with the product: The Muhimbi Workflow Power Pack for SharePoint allows custom C# or VB.NET code to be embedded in SharePoint Designer Workflows without the need to resort to complex Visual Studio based workflows, the development of bespoke Workflow Activities or long development cycles.

The following Blog postings are part of this User Guide series:

  1. Language Features: Discusses the script like syntax, the generic workflow action and condition, passing parameters, returning values from a workflow and using the MyWorkflow property.

  2. Embedding .net code in a Workflow Condition: Provides a number of examples of how to use the Evaluate Custom Code condition to carry out basic as well as complex conditional tasks.

  3. Embedding .net code in a Workflow Action: Contains a number of examples of how to use the Execute Custom Code  to basically carry out any action you can think of in a SharePoint Designer Workflow.

  4. Creating Custom Methods (this article): Shows how to create your own methods in your scripts in order to keep the code organised and easy to maintain.

Due to its scripting like approach, the Workflow Power Pack does not allow regular .NET methods to be created. However, by cleverly using delegates you can create your own reusable pieces of code.

To facilitate this, the following delegates can be used in addition to the normal delegates available in the .net framework. Note that this only works for C# as VB.net does not allow anonymous methods to be created.

delegate void WorkflowMethod(params object[] parameters);

delegate object WorkflowFunction(params object[] parameters);

delegate void WorkflowMethod(params ParameterType[] parameters);

delegate ReturnType WorkflowFunction<ParameterType, ReturnType>(params ParameterType[] parameters);

There is no need to add these delegates to your WPP Code, they are added automatically.

Delegate name

Description

WorkflowMethod

Method with a void return type. Accepts any number of Object based parameters that can be accessed from the delegate body using the parameters array.

Parameters may need to be cast to the correct type before they can be used.

WorkflowFunction

Method using a return type of Object. Accepts any number of Object based parameters that can be accessed from the delegate body using the parameters array.

Parameters may need to be cast to the correct type before they can be used.

WorkflowMethod (Using generics)

Generics based version that allows strongly typed parameters to be passed.

WorkflowFunction (Using generics)

Generics based version that allows strongly typed parameters to be passed and returned

The example provided below creates a generic Debug method to concatenate information to a string. This string is then returned as the workflow’s ReturnValue, from where it can be written to the Workflow History.

string debugString = String.Empty;

WorkflowMethod<string> Debug = delegate(string[] parameters)

{

    debugString += parameters[0] + "\r\n";

}

WorkflowFunction Calculate = delegate(object[] parameters)

{

    return (int)parameters[0] + (int)parameters[1];

}

WorkflowFunction<int, string> Calculate2 = delegate(int[] parameters)

{

    return (parameters[0] + parameters[1]).ToString();

}

Debug("Hello");

Debug("World");

Debug(Calculate(1, 2).ToString());

Debug(Calculate2(3, 4));

MyWorkflow.ReturnValue = debugString;
Author
Clavin Fernandes Developer Relations and Support Services

Clavin is a Microsoft Business Applications MVP who supports 1,000+ high-level enterprise customers with challenges related to PDF conversion in combination with SharePoint on-premises Office 365, Azure, Nintex, K2, and Power Platform mostly no-code solutions.

Explore related topics

Share post
Free trial Ready to get started?
Free trial