Building Custom Activities for Use in SharePoint Designer 2007

Summary: Use the Microsoft Office SharePoint Designer 2007 workflow designer to create custom activities.

Office Visual How To

Applies to: Microsoft Office SharePoint Designer 2007, Windows SharePoint Services 3.0

Patrick Tisseghem, U2U

July 2007

Overview

Microsoft Office SharePoint Designer 2007 allows non-developers to create custom workflows in a straightforward way by using the workflow designer it provides. During the process, the user "glues" together different activities that represent the steps for the workflow. There are numerous activities one can use and the list is extensible. Developers can use Microsoft Visual Studio 2005 to build additional activities and deploy them, so that they are available in Office SharePoint Designer 2007.

See It SharePoint Designer Building Custom Activities

Watch the Video

Length: 16:26 | Size: 12.2 MB | Type: WMV file

Code It | Read It | Explore It

Code It

Preparing the Development Environment

To build custom activities, you must configure your development environment as follows:

We begin by creating a Workflow Activity Library project. You can work with the activity in the designer or in the code editor. An activity can contain one single action or it can group multiple activities together, resulting in a composite activity.

Create Dependency Properties

Properties in the activity are exposed in the workflow designer in Office SharePoint Designer 2007, or the Workflow Designer in Visual Studio 2005, and are populated only when the workflow instance is started at run time. You create and expose these properties by declaring them as type DependencyProperty. Following is the code example for the exposing the TaskTitle property in the CreateTaskInListOnSite activity.

public static DependencyProperty TaskTitleProperty 
    = DependencyProperty.Register
      ("TaskTitle", typeof(string), typeof(CreateTaskInListOnSite)); 

[Category("Cross-Site Actions"), Browsable(true)]
[DesignerSerializationVisibility
  (DesignerSerializationVisibility.Visible)] 
public string TaskTitle  {  get {
    return ((string) 
        (base.GetValue(CreateTaskInListOnSite.TaskTitleProperty))); 
  }
  set {
     base.SetValue(CreateTaskInListOnSite.TaskTitleProperty, value); 
   }
}

The Execute Method

The Windows Workflow run-time engine calls the Execute method of the activity. This method is inherited from the base SequenceActivity class; this is where you write the business logic for the activity. The following code example uses the Microsoft.SharePoint.dll and the object model exposed by it to create a task item in the indicated list in the site that the workflow designer specifies while defining the template.

protected override ActivityExecutionStatus 
   Execute(ActivityExecutionContext executionContext)  {
  try  {
    SPSite sitecollection = new SPSite(this.SiteUrl); 
    SPWeb web = sitecollection.OpenWeb();
    SPUser user = web.Users[this.AssignTo[0].ToString()];
    SPList list = web.Lists[this.ListName]; 
    SPListItem item = list.Items.Add();
    item["Title"] = this.TaskTitle; 
    item["AssignedTo"] = user; 
    item.Update();
  } 
  catch (Exception ex)   {
    EventLog.WriteEntry("MSDN Workflow", ex.ToString());
  }
  return ActivityExecutionStatus.Closed; 
}

The Execute method returns an ActivityExecutionStatus value. In the previous example, this indicates that the work is finished.

.ACTIONS File

The workflow designer in Office SharePoint Designer 2007 communicates with the server running Windows SharePoint Services to retrieve an XML file that contains the options for the user to define conditions and actions while creating the steps of the workflow. These files are language-dependent, and are stored in the path \12\TEMPLATE\1033\Workflow. The WSS.ACTIONS file contains the files that are provided with Windows SharePoint Services. You create an additional file (for example, one named MSDN.ACTIONS) that contains the metadata for your custom activity to expose in Office SharePoint Designer 2007. Following is the XML for this file.

<WorkflowInfo>
  <Actions Sequential="then" Parallel="and">
     <Action Name="Create Task in List On Site"
    ClassName="MSDN.HowTo.CreateTaskInListOnSite"
    Assembly="CustomWorkflowActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a3170baa21b0a3e9"
    AppliesTo="all" Category="Cross Site Actions">
        <RuleDesigner 
           Sentence="Create task titled %1 for %2 on site %3 in 
              list %4">
           <FieldBind Field="TaskTitle" DesignerType="TextArea" 
              Id="1"/>
           <FieldBind Field="AssignTo" DesignerType="Person" 
              Text="this user" Id="2"/>
           <FieldBind Field="SiteUrl" DesignerType="TextArea" Id="3"/>
           <FieldBind Field="ListName" DesignerType="TextArea" Id="4"/>
        </RuleDesigner>
         <Parameters>
            <Parameter Name="TaskTitle" Type="System.String, mscorlib" 
              Direction="In" />
            <Parameter Name="AssignTo" Type="System.String, mscorlib" 
              Direction="In" />
            <Parameter Name="SiteUrl" Type="System.String, mscorlib" 
              Direction="In" />
            <Parameter Name="ListName" Type="System.String, mscorlib" 
              Direction="In" />
         </Parameters>
     </Action>
  </Actions>
</WorkflowInfo>

Authorizing the Custom Workflow Activity

You must register the assembly that contains the custom activity as an AuthorizedType in the web.config file that is at the root of the Internet Information Services (IIS) server Web Application folder. Following is the entry to add in the authorizedTypes section.

<authorizedType Assembly="CustomWorkflowActivities, Version=1.0.0.0, 
   Culture=neutral, PublicKeyToken=a3170baa21b0a3e9"     
   Namespace="MSDN.HowTo" TypeName="*" Authorized="True" />

Read It

To make a custom activity available for the workflow designer in Office SharePoint Designer 2007, follow these steps:

  1. Use Visual Studio 2005 to create a Workflow Activity library project containing a class that inherits from the System.Workflow.Activities.SequenceActivity class.

  2. Expose properties as DependencyProperty types if you want the workflow engine to interact with it at run time.

  3. Override the Execute method to code the logic of the activity that returns a status value back to the workflow runtime engine.

  4. Sign the project and compile it in a .NET assembly to deploy in the global assembly cache.

  5. Create an .ACTIONS file containing the metadata for the custom activity that is loaded by the workflow designer in Office SharePoint Designer 2007.

  6. Authorize the custom activity in the web.config file for the targeted IIS Web application.

Explore It