Subscribe to News feed

SharePoint Workflow Power Pack User Guide – Creating custom methods

Posted at: 14:17 on 09 February 2010 by Muhimbi

User-Guide 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<ParameterType>(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;

 






Labels: , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

Need support from experts?

Access our Forum

Download Free Trials