Custom Workflow Actions (Part 1 of 3)

A recent SharePoint project on my plate had a specific requirement that every item added to a list must be emailed to a specific email address. At the time of the meeting, this requirement seemed to be a simple requirement. While stating that the requirement was possible, which was based on a vague memory of emailing functionality in the SharePoint Designer workflow action list, I decided to agree on making it happen.<!–more–>

What I did not realize was that the workflow actions in SharePoint Designer are quite basic. I had built up in my mind that the workflow capabilities of SharePoint were powerful from what I heard from so many individuals. What I did not realize was, while the fact that SharePoint workflows are powerful, to make them perform unique tasks it would require considerable involvement.

Unfortunately for me, I had a deadline to complete the task within two days. Thus began the conquering of creating a custom workflow action. Since much of the information I found on the Internet seemed to be missing pieces of the puzzle, here is my attempt to create a complete tutorial on creating a custom workflow action in Visual Studio 2008 that is applicable in SharePoint Designer and Windows SharePoint Services.

Requirements

If programming for SharePoint is new to you, patience will serve you well. I was very frustrated for the two days while I figured out and then produced what I am going to explain to you below.

Fundamentals

The workflow functionality of SharePoint is based on the Windows Workflow Foundations (WWF) programming model, therefore if you have experience with WWF you may find programming workflow actions for SharePoint intuitive. Microsoft Windows SharePoint Services (WSS) is built on .NET Framework technology, which we will be utilizing for the workflow action program. If you understand both of these technologies well, creating a custom workflow action will be a breeze.

Creating a Custom Workflow Action

Enough with the preliminary text, let’s get started with creating the custom workflow action.

For the demonstration, we will be creating a custom workflow action that allows you to send an email with the attachments from a specified list in your site. The custom workflow action will allow you to configure four user input fields: (a) the Email Content; (b) the Send From Email; (c) the SMTP Host; and (d) the Associated List.

Although this project can be broken down into three sequential steps, these three steps are so closely intertwined that content from all three are required by the other three. With that stated, read all three steps, play around, then begin your actual development. The three steps are:

Step One: Create and Compile your .NET Workflow Activity

Project Environment Setup

Let’s begin by setting up the Visual Studio project and the environment, so open Visual Studio 2008.

In Visual Studio 2008, start a new Project by clicking on File > New Project.

In the New Project window, choose Workflow Activity Library and name the activity the same name you will use for the rest of the project. For the purposes of this demonstration, we will name the project devtrends.EmailAttachments. Click OK.

Since a custom workflow action does not have any graphical interface and in addition we are creating a single workflow activity, go ahead and immediately view the code for the project.

Next, we will add the references to two DLLs that are associated with the installation of Windows SharePoint Services (WSS). These files will reside on the server that runs WSS, which is likely in the path: \\server_name\c$\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\

Now that those files are copied to your local development computer, return to the Visual Studio project and add these two DLLs as references.

Now, add a few “using” statements to include the required namespaces. The namespace list for this project is as shown below, with the additional statements added at the end of the “using” line up.

Just two more changes, as shown as below: (a) change the name of the partial class to “SentEmailwithListAttachments” by modifying the first line to be (ensure that you are not editing the Designer.cs file); and (b) Add the five Using statements, Microsoft.SharePoint, Microsoft.SharePoint.Workflow, Microsoft.SharePoint.WorkflowActions, System.Net.Mail and System.IO.

Now that we are ready to create the SharePoint Workflow Activity, let’s begin by copying and pasting the following code into your partial class, SendEmailwithListAttachments:

IT is not just a job but also a passion. Everything I have accomplished, both personally and professionally, has been generally entertaining, bordering on fun. Some of my projects, such as working with SharePoint Services workflow actions in Visual Studio or building a custom iSCSI SAN using the OpenSolaris, ZFS and COMSTAR, has been quite rewarding. You may think nerd...I think developing a new trend!

Hi i am getting this error what do you this is about?
‘TestingWorkflow.SentEmailwithListAttachments.Execute(System.Workflow.ComponentModel.ActivityExecutionContext)’: no suitable method found to override.

Look at your code, specifically the .Designer.cs file, and verify that the partial class name matches that in the .cs file. Also, in the .cs file, check the function in the “override” region and verify that the name/class matches there.