Execute custom code

⌘K

This activity enables the execution of custom C# or VB.NET code from within SharePoint Designer Workflows.

execute_custom_code_1

You can use this activity to invoke custom C# or VB.NET code in SharePoint Designer without needing to create complex Visual Studio workflows. The activity follows a script-like approach, so there’s no need to define namespaces, classes, or method names.

Note: The code should include the using (imports) section and code lines without method (Sub procedure) declarations.

Note: This activity uses .NET 3.5 to compile custom code, so all .NET 3.5 features are available within the code.

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

Example Code
C#:

execute_custom_code_2

[VB.NET]

execute_custom_code_3

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

You can embed Workflow Lookup variables into the code. At runtime, all lookup variables are replaced with text, so please ensure the generated syntax remains valid.

For example, the following code may not execute correctly:

string userFirstName = [%Variable: firstName%];

This could result in:

string userFirstName = Frank;

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

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

This will result in the following syntactically correct code:

string userFirstName = "Frank";

The Context variable includes several properties for use in your code:

Properties
PropertyTypeDescription
WorkflowContextWorkflowContextGets the workflow context.
CurrentListSPListGets the current SPList object.
CurrentListItemSPListItemGets the current SPListItem object
InParameter1ObjectGets the incoming parameter #1
InParameter2ObjectGets the incoming parameter #2
CurrentActivityActivityGet the current activity.
WorkflowInstanceIdGuidGets the Guid associated with the workflow instance.
ActivationPropertiesSPWorkflowActivationPropertiesRepresents the initial properties of the workflow instance as it starts.
ResultObjectGets or sets the output result.

The following Assembly References are 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: The assembly should be installed in the Global Assembly Cache (GAC).

You can add or remove references to custom assemblies. The string format must follow the full assembly name.

Note: A 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.Activities and store result in result.

Parameters

ParameterDescription
this codeC# or VB.NET code
C#Type of code: C# or VB.NET.
parameter1Optional. The incoming parameter #1. Default value is null.
parameter2Optional. The incoming parameter #2. Default value is null.
Virto.Sharepoint.ActivitiesOptional. List of referenced assemblies in the full assembly name format, e.g., System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.

How can we help?