Posted at: 3:27 PM on 22 December 2009 by Muhimbi
In part 3 of our series of User Guide related blog postings for the Muhimbi Workflow Power Pack for SharePoint we provide an example of how to embed c# or vb code directly into a SharePoint Designer Workflow Action.
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:
- 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.
- 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.
- Embedding .net code in a Workflow Action (this article): 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.
- Creating Custom Methods: Shows how to create your own methods in your scripts in order to keep the code organised and easy to maintain.
SharePoint ships with a number of Workflow Actions to carry out basic tasks such as setting the value of an item or sending an email. However, if you need to do something slightly different that is not supported by any of the stock actions, you need to resort to expensive third party utilities that may match some of your requirements, but probably not all of them.
This section describes how to use the Muhimbi Workflow Power Pack to implement your exact requirements without any limits or dependencies on third party logic. See part 2 of this series for another example that automatically renames a file based on keywords in its name.
Reading a SQL Database using a Workflow Action
Although SharePoint can be used to store much of your data, in reality a typical enterprise stores data in all kind of formats and data stores.
In this example we’ll discuss how to use the Muhimbi Workflow Power Pack to retrieve data stored in a SQL Server Database. The example is not particularly practical as we just read a group name out of the SharePoint Content Database, however the same principle can be used for any database.
To create this workflow, carry out the following steps:
- We need to be able to access functionality in the System.Data assembly. Add this reference to the relevant Web Application using the Workflow Power Pack Central Administration screens as described in the Administration Guide.
- Make sure you have the appropriate privileges to create workflows on a site collection.
- Create a new workflow using SharePoint Designer.
- On the Workflow definition screen associate the workflow with your list of choice (any list will do), tick the two “automatically start” check boxes and proceed to the next screen.
- Click the Actions button and insert the Execute Custom Code action.
- Click this variable and create a new Workflow Variable named groupName using string as the type.
- Insert the following C# based code by clicking this code.
// ** Get the connection string for the content DB
string connString =
using(SqlConnection connection = new SqlConnection(connString))
// ** Execute the Query
string sql = "SELECT TOP (1) Title FROM Groups";
SqlCommand cmd = new SqlCommand(sql, connection);
// ** Store the result in the ReturnValue
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read() == true)
MyWorkflow.ReturnValue = reader["Title"].ToString();
- Click the Actions button, select Log to History List, click this message, set the Source to Workflow Data and the Field to groupName.
- Close the Workflow Designer and add / update an item in the list to trigger the workflow.
- Once the workflow has finished, click the Completed link to see the output.
We are connecting to the content database in this example to make it easier to execute as no new databases or connection strings need to be created.