In Microsoft Visual Studio 2008, you can create custom activities that can be consumed in workflows. Creating custom activities lets you encapsulate business logic that applies to different scenarios and that can be used in different workflows. This Microsoft Office Visual How To demonstrates how to create a custom activity to send an e-mail message with an attachment.
First, you must create your custom activity. Then, you can add that to a workflow project and use it to send an e-mail message with an attachment.
Creating a Custom Activity Project
First, create a Visual Studio 2008 Workflow Activity Library project.
To create a Visual Studio 2008 Workflow Activity Library project
Open Visual Studio 2008.
On the File menu, point to New, and then click Project.
Under the Workflow Project Type, select the Workflow Activity Library project template.
In the Name box, type SendMailWithAttachmentActivity, and then click OK.
Rename Activity1 to SendMailWithAttachmentActivity.
Coding Custom Activity
Now that you have a Custom Activity project, you must add code to send an e-mail message with an attachment.
To add code to send an e-mail message with an attachment
The first thing you must do is change your class declaration to inherit directly from Activity instead of SequenceActivity.
Open your activity in code view, and change SequenceActivity to Activity. Your class definition should look like the following.
Now you must create some Dependency Properties. Add the following fields inside your class definition.
public static DependencyProperty ToProperty
= DependencyProperty.Register(
"To", typeof(string), typeof(SendMailWithAttachmentsTest));
public static DependencyProperty FromProperty
= DependencyProperty.Register(
"From", typeof(string), typeof(SendMailWithAttachmentsTest));
public static DependencyProperty CCProperty
= DependencyProperty.Register(
"CC", typeof(string), typeof(SendMailWithAttachmentsTest));
public static DependencyProperty SubjectProperty
= DependencyProperty.Register(
"Subject", typeof(string), typeof(SendMailWithAttachmentsTest));
public static DependencyProperty BodyProperty
= DependencyProperty.Register(
"Body", typeof(string), typeof(SendMailWithAttachmentsTest));
public static DependencyProperty AttachmentProperty
= DependencyProperty.Register(
"Attachment", typeof(string), typeof(SendMailWithAttachmentsTest));
public static DependencyProperty SmtpServerProperty
= DependencyProperty.Register(
"SmtpServer", typeof(string), typeof(SendMailWithAttachmentsTest));
public static DependencyProperty InvokeEvent
= DependencyProperty.Register(
"Invoke", typeof(EventHandler), typeof(SendMailWithAttachmentsTest));
Notice that the last field, InvokeEvent, is of type EventHandler. This enables you to add custom code to your activity when it is used in a workflow.
Now, you must add properties for your fields. Add the following code under the fields you just added.
Now, you must add a property for your InvokeEvent field. This EventHandler property lets you add code in a workflow to interact with this activity programmatically. Add this code under the Properties you just added.
Add a using statement at the top of the class. This enables you to access the ASP.NET 2.0 Mail namespace for sending your e-mail message. Add the following code.
The last thing you must do here is override the Execute method. Add the following method inside your class definition.
Installing a Custom Activity
Before you can use your custom activity, you must install it in the global assembly cache (GAC). For an assembly to be installed in the GAC, you must give it a strong name.
To install the custom activity to the GAC
First, give your assembly a strong name. In Solution Explorer, right-click the project, and then select Properties.
Select the Signing tab.
Select the Sign the assembly check box.
In the Choose a strong name key file list, select New.
In the Key file name text box, type Key.
In this demonstration, we are not concerned with security, so clear the Protect my key file with a password check box. Click OK.
Now your assembly is strong named, and all you have to do is install it to the global assembly cache.
Save and build the project.
Open a Visual Studio 2008 command prompt.
Navigate to the Activity solution directory, and run the following command:
gacutil /if SendMailWithAttachmentActivity.dll
Now, that you have successfully installed your activity, you can use it from a workflow project.
Creating a Workflow Project
Next, you create a Sequential Workflow project to use your custom activity.
To create a Visual Studio 2008 Sequential Workflow project
Start Visual Studio 2008.
On the File menu, point to New, and then click Project.
Under the Office Project Type, select the SharePoint 2007 Sequential Workflow project template.
In the Name box, type SendEmailWithAttachmentWF. Click OK.
Type a valid SharePoint Web URL. The example uses http://moss.litware.com . Click Next.
Select a library or list to associate with your workflow, and click Next.
Leave the default options for how a workflow can be started, Manually by users and When an Item is created. Click Finish.
Adding a Custom Activity to the Toolbox
Now that you have created your Sequential Workflow project, you must add your custom activity to your Toolbox before you can use it.
On the Tools menu, select Toolbox items.
Select the Activities tab.
Click Browse, and browse to the SendMailWithAttachmentActivity.dll in your Activity solution directory. Click Open.
Click OK.
Your activity is now available in the Toolbox.
Figure 1. Toolbox with new activity available
Note
If you have a toolbox category selected, the activity is added to that category by default. You can drag the activity to another category if you want.
Adding a Custom Activity to the Workflow
Now, you must add your custom activity to your simple workflow.
To add the custom activity to the workflow
From the Toolbox, drag the SendMailWithAttachmentActivity to be under the onWorkflowActivated1 activity.
Open the Properties window, and rename it to sendMailWithAttachmentActivity.
You can set the activity properties from within the Properties window. Instead, you will do that in code. Select the Event view in the Properties window.
You should see a single event, Invoke. Double-click this property.
Figure 2. Properties window
This takes you to the code-behind class, and you will see that a method stub has been auto-generated for you.
Add the following code inside this method.
Note
The purpose of this demonstration is to show you how to create and consume a custom activity. In a real-world scenario, you would never hard-code these values. You would retrieve them programmatically, or possibly through an Initiation form or a Task form.
Running the Workflow Project from Visual Studio 2008
Press F5 to run your workflow. When you activate this workflow on a document in a document library, an e-mail message is generated and sent with the attachment you specified.
Figure 3. E-mail message with attachment
This Microsoft Office Visual How-To demonstrates how to create a simple custom activity. Custom activities enable you to encapsulate business logic in an efficient, reusable manner. You can update this custom activity to include validation, toolbox designer graphics, and the ability to include more than a single attachment. |
Watch the Video
Video Length: 00:09:06
File Size: 8.19 MB WMV
|