Designing Sharepoint Designer Workflow Activities

by

Oliver here, today with some information about what steps are necessary to use your workflow activity in Sharepoint Designer.

Designing Activities so that they can be used in Sharepoint Designer (SPD) consists of four parts:

  1. Preparations
  2. Code the properties for the connection to SPD
  3. Define the display in SPD in a xml file
  4. Code the activity

1. Preparations

Create a project of the Type Workflow -> Workflow Activity Library

newproject

You should sign the assembly with a strong name key. To do so, choose the properties of your project and choose or create a strong name key under the tab “signing”.

Within your solution you’ll find an Activity class called Activity1. Right click it and choose “View Code”. Your class should inherit from SequenceActivity.

2. Defining the Properties

This example shows an activity which is used to send mails, as already present in SPD, but with the additional option of defining a custom “Reply To”-address.

To define a property for use in SPD we have to create Dependency Properties and map them to code properties. In the example below we’ll define the To and Subject property of the mail, as well as the special properties __Context and __ActivationProperties which will be used to reference our Sharepoint Server executing the activity.

public static DependencyProperty ToProperty
      = DependencyProperty.Register(
          "To", typeof(System.Collections.ArrayList), typeof(MailWithReplyToAction));

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[ValidationOption(ValidationOption.Required)]
[Category("Adresses"), Browsable(true)]
[Description("Enter the recipients, separated by comma.")]
public System.Collections.ArrayList To
{
get { return ((System.Collections.ArrayList)(base.GetValue(MailWithReplyToAction.ToProperty))); }
set { base.SetValue(MailWithReplyToAction.ToProperty, value); }
}

public static DependencyProperty SubjectProperty
      = DependencyProperty.Register(
          "Subject", typeof(string), typeof(MailWithReplyToAction));

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[ValidationOption(ValidationOption.Optional)]
[Browsable(true)]
[Description("Enter the subject of the mail")]
public string Subject
{
get { return ((string)(base.GetValue(MailWithReplyToAction.SubjectProperty))); }
set { base.SetValue(MailWithReplyToAction.SubjectProperty, value); }
}

public static DependencyProperty __ContextProperty
      = DependencyProperty.Register(
          "__Context", typeof(WorkflowContext), typeof(MailWithReplyToAction));

[Description("The site context")]
[Category("Context")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public WorkflowContext __Context
{
get { return ((WorkflowContext)(base.GetValue(MailWithReplyToAction.__ContextProperty))); }
set { base.SetValue(MailWithReplyToAction.__ContextProperty, value); }
}

public static DependencyProperty __ActivationPropertiesProperty
      = DependencyProperty.Register(
          "__ActivationProperties", typeof(SPWorkflowActivationProperties), typeof(MailWithReplyToAction));

[Description("The activation properties")]
[Category("Activation Properties")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public SPWorkflowActivationProperties __ActivationProperties
{
get { return ((SPWorkflowActivationProperties)(base.GetValue(MailWithReplyToAction.__ActivationPropertiesProperty))); }
set { base.SetValue(MailWithReplyToAction.__ActivationPropertiesProperty, value); }
}

The To property is of type ArrayList. This is for use with the Designer Type E-Mail, which we will use to enter our data. Note that the Subject is optional, but this definition alone is not enough, we have to tell that to the SPD explicitly through the XML file, which we’ll define in the next step.

3. Define the display in SPD in a xml file

SPD shows the activities in sentences, and to tell the Sharepoint Designer how it should display the activity and what fields it requires etc, wel’ll create a xml file ending in “.actions”. These files go to the 12-Hive in the directory TEMPLATE\1033\Workflow. Note that 1033 is the default for english Sharepoint Installations and may vary depending on the language used. SPD will read the “.actions” file when a user wants to create a workflow. SPD does not care for the name, but for simplicity’s sake I would strongly recommend naming them after your solution.

Here is my xml file called SendMailWithReplyTo.actions

<WorkflowInfo>
 <Actions Sequential="then" Parallel="and">
 <Action Name="Send Mail with Reply To"
 ClassName="SendMailWithReplyToActivityFeature.Features.SendMailWithReplyTo.MailWithReplyToAction"
 Assembly="SendMailWithReplyToActivityFeature, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234567890"
 AppliesTo="all" Category="CAIRO Actions">
 <RuleDesigner Sentence="Send Mail %1 with Reply-To %2">
 <FieldBind Field="To,CC,Subject,Body" Text="this mail" DesignerType="Email" Id="1"/>
 <FieldBind Field="ReplyTo" Text="this address" DesignerType="TextArea" Id="2"/>
 </RuleDesigner>
 <Parameters>
 <Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext, Microsoft.SharePoint.WorkflowActions" Direction="In" />
 <Parameter Name="__ActivationProperties" Type="Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties, Microsoft.SharePoint" Direction="Out" />
 mscorlib" Direction="Optional" />
 mscorlib" Direction="In" DesignerType="StringBuilder"/>
 mscorlib" Direction="Optional" DesignerType="StringBuilder"/>
 mscorlib" Direction="Optional" />
 mscorlib" Direction="Optional" DesignerType="StringBuilder"/>
 </Parameters>
 </Action>
 </Actions>
</WorkflowInfo>

Note that the DesignerType “Email” is bound to four fields in the FieldBind section of the sentence. This DesignerType requires the To and CC field to be of the type System.Collection.ArrayList. In the Parameter section note that the Direction must be set to Optional for optional parameters or SPD will ask you to enter them, no matter whether you set them to required in the source code or not.
For the __ActivationProperties the Direction has to be set to “Out”.
With the Properties defined and ready to be displayed in SPD, the last step is to code the actual activity, which is rather easy compared to the overhead so far :)

4. Code the activity

To code the activity, override the execute method of the SquenceActivity base class. I will give some examples when to use our __Context and __ActivationProperties properties.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
 {

//here goes the implementation

// using the  __Context we can reference our site or web

// furthermore, we can get the hostname of the outgoing smpt server

SmtpClient smtp = new SmtpClient();

smtp.Host = __Context.Site.WebApplication.OutboundMailServiceInstance.Server.Address;

// if we want to know more about our current workflow, for example to create history events, we can use the __ActivationProperties

SPWorkflow currentWorkflow = __ActivationProperties.Workflow;

// at the end, if all worked correctly, we have to inform the workflow engine about our success:

return ActivityExecutionStatus.Closed;

}

I used SPVisualDev and WSP Builder to build a solution package that deploys my assembly and the .actions file to their destination and creates a feature reciever to register the assembly as safe in the web.config.

And that’s it. Hope you found this article helpful!

Tags: , , , ,

4 Responses to “Designing Sharepoint Designer Workflow Activities”

  1. Out of the box Sharepoint Workflows | Weaver's Loom Says:

    [...] Designing Sharepoint Designer Workflow Activities (splog.cairo.ag) This entry was posted in Office, Sharepoint, technology, web and tagged Consulting, Document Management, Microsoft, Microsoft SharePoint, Microsoft Visual Studio, Products, Sharepoint, Workflow. Bookmark the permalink. ← Converting an Access DB to Sharepoint Facebook Annoyances … → [...]

  2. Rick Says:

    Hi Oliver,
    Would you be so kind to as to create a second part to this interesting blog explaining step by step how to build the solution package for a custom workflow activity using SPVisualDev and WSPBuilder? I have just started learning SP development and I have been trying to create one for weeks without success… :(
    Please help me.
    Thanks.
    - Rick

    • Oliver Grahm Says:

      Hi Rick,
      This is the short version: I created an empty SPVisualDev solution, added a feature without artifacts, and added my source code of the activity via “add existing item” in visual studio. I had to change my namespaces accordingly. After that, i used the SPVisualDev feature manager to add an feature reciever.

      Since SPVisualDev Projects mirror the folder structure of Sharepoint 2007 you can place the files where they would need to go on the server, e.g. put the .actions file under TEMPLATE\1033\Workflow.

      Hope this helps,
      Oliver

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.