Building Custom Activities for Use in SharePoint Designer 2007
Summary: Use the Microsoft Office SharePoint Designer 2007 workflow designer to create custom activities.
Applies to: Microsoft Office SharePoint Designer 2007, Windows SharePoint Services 3.0
Patrick Tisseghem, U2U
July 2007
Code It | Read It | Explore It
Code It
To build custom activities, you must configure your development environment as follows:
Install Visual Studio 2005
Install Visual Studio 2005 Extensions for the .NET Framework 3.0 (Windows Workflow Foundation).
Provide access to the path
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\Workflow
on the server running Windows SharePoint Services 3.0
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.
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 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.
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>
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:
Use Visual Studio 2005 to create a Workflow Activity library project containing a class that inherits from the System.Workflow.Activities.SequenceActivity class.
Expose properties as DependencyProperty types if you want the workflow engine to interact with it at run time.
Override the Execute method to code the logic of the activity that returns a status value back to the workflow runtime engine.
Sign the project and compile it in a .NET assembly to deploy in the global assembly cache.
Create an
.ACTIONS
file containing the metadata for the custom activity that is loaded by the workflow designer in Office SharePoint Designer 2007.Authorize the custom activity in the web.config file for the targeted IIS Web application.