Blog Post

SharePoint Workflow Power Pack User Guide — Embedding .NET Code in a Workflow Action

Clavin Fernandes
Illustration: SharePoint Workflow Power Pack User Guide — Embedding .NET Code in a Workflow Action

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:

  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 (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.

  4. 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:

  1. 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.

  2. Make sure you have the appropriate privileges to create workflows on a site collection.

  3. Create a new workflow using SharePoint Designer.

  4. 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.

  5. Click the Actions button and insert the Execute Custom Code action .

  6. Click this variable and create a new Workflow Variable named groupName using string as the type.

  7. Insert the following C# based code by clicking this code.

    using System.Data.SqlClient;
    
    // ** Get the connection string for the content DB
    
    string connString =
    
                MyWorkflow.Site.WebApplication.ContentDatabases[0].DatabaseConnectionString;
    
    using(SqlConnection connection = new SqlConnection(connString))
    
    {
    
        connection.Open();
    
        // ** 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();
    
    }
  8. Click the Actions button, select Log to History List, click this message, set the Source to Workflow Data and the Field to groupName.

  9. Close the Workflow Designer and add / update an item in the list to trigger the workflow.

  10. Once the workflow has finished, click the Completed link to see the output.

    SQLAccess

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.

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