Menu
SharePoint Workflow Extensions Kit

Execute custom code

This activity allows executing a custom C# or VB.NET code from SharePoint Designer Workflows.

You can use this activity to invoke custom C# or VB.NET code from SharePoint Designer with no need to create complex Visual Studio workflows. Don’t worry about namespaces, class or method name, the activity takes a script like approach.

Note: The code should include using (includes) section and code lines without method (Sub procedure) declaration.

Note: The activity uses .NET 3.5 to compile custom code. So, all .NET 3.5 futures are available from the code.

The following example reads XML from parameter1, selects node by XPath from parameter2 and stores string result to output variable.

[C#]

using System.Xml;

 

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml((string)Context.InParameter1);

XmlNode node = xmlDoc.SelectSingleNode((string)Context.InParameter2);

Context.Result = node.InnerText;

[VB.NET]

Imports System

Imports System.Xml

Dim xmlDoc As XmlDocument = new XmlDocument()

xmlDoc.LoadXml(Context.InParameter1 As String)

Dim node As XmlNode = xmlDoc.SelectSingleNode(Context.InParameter2 As String)

Context.Result = node.InnerText

You should use Context variable to access incoming parameters and return result from your custom code.

You can add Embedding Workflow Lookup variables to the code. At runtime all lookup variables are placed into the code as text, so please make sure that the generated syntax is still valid.

For example, the following code will not execute correctly:

string userFirstName = [%Variable: firstName%];

As this may result in the following code:

string userFirstName = Frank;

To solve this problem, add quotes around the variable as follows:

string userFirstName = "[%Variable: firstName%]";

Which will result in to the following, syntactically correct, code:

string userFirstName = "Frank";

The Context variable includes several properties which you can use in the code:

Properties

Property Type Description
WorkflowContext WorkflowContext Gets workflow context.
CurrentList SPList Gets the current SPList object.
CurrentListItem SPListItem Gets the current SPListItem object
InParameter1 Object Gets the incoming parameter #1
InParameter2 Object Gets the incoming parameter #2
CurrentActivity Activity Get the current activity.
WorkflowInstanceId Guid Gets the Guid associated with the workflow instance.
ActivationProperties SPWorkflowActivationProperties Represents the initial properties of the workflow instance as it starts.
Result Object Gets or sets the output result.

The activity makes the following Assembly References available by default:

Virto.Sharepoint.Activities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7b70ddd211dc63c8

System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

System.Workflow.Componentmodel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c

Microsoft.SharePoint.Workflows, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c

Microsoft.SharePoint.WorkflowActions, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c

Note: Assembly should be installed in the global assembly cache (GAC).

You can add (remove) reference to the custom assemblies. The string line is long form of assembly name.

Note: Reference to the Virto.Sharepoint.Activities assembly is required.

SharePoint Workflow Designer Phrase

Execute this code written in C#, passing incoming parameters parameter1 and parameter2, use referenced assemblies Virto.Sharepoint.Acti… and store result in result.

Parameters

Parameter Description
this code C# or VB.NET code
C# Type of the code. C# or VB.NET
parameter1 Optional. The incoming parameter #1. Default value is null.
Parameter2 Optional. The incoming parameter #2. Default value is null.
Virto.Sharepoint.Acti… Optional. The list of referenced assemblies. The string line is long form of assembly name. Example: System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089