In a project I have been involved with, our customer wants to send an email with an attachmed document file.
As some among you which have landed on this page already know, the security policies of Microsoft don't allow to send attachments from an email within a SharePoint workflow. Thus, we have to develop some code for implementing this functionality.
Personally I've used Visual Studio 2010 (VS2010), SharePoint Designer 2010 (SPD2010) and the useful insights of Dave Stuart.
You may find his own blog here: Dave Stuart's blog
Most of the code that follows, it owes much to this Patrick Bloom's article on how to develop a Custom Email activity for SharePoint Designer 2010
In order to deliver the solution we need to follow the following steps:
- Set up a standard SharePoint solution in VS2010;
- Define our custom ACTIONS file, which tell SharePoint and SPD2010 which activities to include;
- Define our SendMailActivity class, which will implement the action specified in the ACTIONS file;
- Package, deploy and test.
First of all, create a new Visual Studio solution using the SharePoint empty project template.
Now we can add a SharePoint Mapped Folder that point to the following location:
This folder is were the ACTIONS file related to the workflow are stored.
SharePoint 2010 comes out of the box with its own file called WSS.ACTIONS. This file contains all the standard actions you may find in SharePoint Designer.
You may add custom actions to this file, but personally I preferred to create a new ACTIONS file as shown below.
2 Define our custom ACTIONS file
Now we can populate the ACTIONS file (basically an XML document) with the information that desscribe our custom action. It should be noticed that we can add several actions in an ACTIONS file, I needed just one.
Let’s discuss the contents. The WorkflowInfo node just indications this definition applies to Workflows. The Actions node defines how the text is build up in the workflow designer. For example, if used in a sequential action, the action will read <actiondescription> then.
The actual action starts with the Action node. In there, the attributes Name, ClassName, Assembly, AppliesTo and Category define the name of the action in the SPD2010 actions menu, the class and assembly for the code, if it relates to list items, documents only or all, and the Category in which it is listed in the actions menu.
The RuleDesigner node specifies the text shown in the editor and related input parameters. The attribute Sentence specifies the sentence shown in the designer. Unlike Boom's one, my custom activity doesn't accept input parameters because I preferred another implementation. That's why the Parameters node specifies only the workflow context parameters.
Now the hard part begins.
3 Define our custom SendEmailActivity class
Add a new class to your project. Call the class in a meaningful way (i.e. SendEmailActivity) as stated in the ACTIONS file. Have the class inherit from the System.Workflow.ComponentModel.Activity class.
Now for each of the parameters we stated in the ACTIONS file, we need to create properties and ensure that they map. As I stated before, my ACTIONS file uses only the Workflow Context properties; and the custom activity class will do the same. The Workflow Context properties use a DepencyProperty to map the property in the class to the parameter stated in the ACTIONS file.
The using statements and the Workflow Context properties code snippets follow:
Please note that the name of each DependencyProperty is equal to the name of the property in the class, appended with Property. Also, the name of the property should be equal to the name of the parameter in the ACTIONS file.
Now it's the turn of the method that actually performs our custom activty.
As aforementioned, our custom class inherits from hhe base class System.Workflow.ComponentModel.Activity, which contains a method called Execute. That method represents the entry point for our custom activity. To implement, override the method in your custom activity. See the implementation of the method below:
To walk through the method, we first get the current web via its URL (we could use the Workflow Context property as well). Then we get the SP SMTP information and we build the message with to and from info, body and subject. The next step is to download our attachment and we add it to the email message (in my project a SP List Item has just an attachment, that's why we take the URL for just the first attachment of the Item). Finally we can send the email message.
Before you can use your custom action, you need to register it with SharePoint in the web.config for the farm in which we are going to install the feature. We therefore use a feature receiver to do this when activated. Add a feature to your solution and attach a feature receiver. Implement the receiver as follows:
Be sure the Deploy Configuration for VS2010 will deploy the package on GAC so when you'll deploy the solution you should expect to find a dll file there.
Futhermore VS2010 will add the assembly details to the web.config automatically as follow:
Perform an iisreset and then launch SPD2010. The new custom activity should be listed among the other actions.
That's all folks.