Workflow, Collaboration, Enterprise Content Management

Add Your Own Custom Workflow Activities to SharePoint Designer 2007

by John Holliday 27. March 2007 09:24
Update (4/4/2007):  I've just learned that another SharePoint blogger has written about this very topic 3 months ago.  The weird part is that the custom action he created also writes to the event log.  In all honesty, this was sheer coincidence (I really should learn to do a search before posting!).  In the interest of fairness and in the spirit of giving credit where it is due, you should check out the following articles by fellow SharePoint MVP Carlos Segura Sanz:
 
http://www.ideseg.com/AddCustomWorkflowActivitiesToSharePointDesignerPart1.aspx
http://www.ideseg.com/AddCustomWorkflowActivitiesToSharePointDesignerPart2.aspx

If you've used SharePoint Designer 2007 to create custom workflows for SharePoint lists, then you know it's a powerful tool for automating business processes. The built-in workflow designer lets you construct fairly sophisticated workflows without writing any code at all. Using this tool to create a workflow is about as easy as setting up a custom rule using the Outlook rules wizard.

Out of the box, you get about two dozen or so activities that you can use to construct your workflows. These activities are pretty comprehensive and let you do things like create and modify list items, send emails, create todo items, etc. But what if you need to do other things? What if you've written some custom activities that, for example, talk to your back-end systems or perform some highly specialized operation?

Well, there's good news. You can extend SharePoint Designer 2007 so you can reference your own custom activities directly from the workflow designer. What's nice about this is that it gives you a tool that both developers and business analysts can use when designing a business process automation solution. The developer can focus on creating libraries of custom activities while the business analyst or administrator focuses on the high-level logic that invokes the activities to do the actual work.

WSS.ACTIONS

What makes this work is a file on the SharePoint server named WSS.ACTIONS. This file is located in the 12\TEMPLATE\1033\Workflow folder. Whenever you open or create a workflow in SharePoint Designer, this file is downloaded to configure the workflow designer interface. It describes the kinds of activities that can be used in a declarative workflow, along with detailed information about how to present the rules you will use to build conditions, specify actions and interact with each activity. By editing the information in this file, you can completely customize the user interface presented in the workflow designer.

The steps to add a simple activity to the SharePoint Designer interface are:

  1. Create a custom activity assembly,

  2. Sign and deploy the activity assembly to the GAC,

  3. Configure SharePoint to recognize the custom activity,

  4. Create a .ACTIONS file to be used by SharePoint Designer

Creating a Custom Activity

The first step is to identify or create the custom activity you want to surface in the designer. The following code gives an example of a simple activity that writes a message to the system event log.

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;


namespace MyCustomActivity
{
  public partial class EventLogger: Activity
  {
    public EventLogger()
    {
      InitializeComponent();
    }

    public static DependencyProperty MessageProperty
      = System.Workflow.ComponentModel.DependencyProperty.Register(
      "Message", typeof(string), typeof(EventLogger));

    [Category("MyCustomActivity"), Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string Message
    {
      get
      {
        return ((string)(base.GetValue(EventLogger.MessageProperty)));
      }
      set
      {
        base.SetValue(EventLogger.MessageProperty, value);
      }
    }

    protected override ActivityExecutionStatus
      Execute(ActivityExecutionContext executionContext)
    {
      using (EventLog log = new EventLog("MyCustomActivity"))
      {
        try
        {
          log.Source = "EventLogger Activity";
          log.WriteEntry(this.Message, EventLogEntryType.Information);
        }
        catch
        {
        }
      }
      return ActivityExecutionStatus.Closed;
    }
  }
}

In this code, the Message property is used to specify the text that will be displayed in the event log, and we use a dependency property to enable the workflow to bind data to it. As with all workflow activities, the Execute method performs the action.

Deploying and Registering the Activity

After you build the custom activity assembly, sign it and copy it to the GAC. You then have to tell SharePoint to trust the assembly. This is similar to configuring a web part as a safe control, but instead of adding an entry to the <SafeControls> section, you add an entry to the <System.Workflow.ComponentModel.WorkflowCompiler> section. Edit the web.config file for your SharePoint web application and add an <authorizedType> element as in the following example:

<authorizedType Assembly="JohnHolliday.Workflow.EventLoggerActivity, Version=1.0.0.0,
      Culture=neutral, PublicKeyToken=0b97b340d4a71524"
      Namespace="MyCustomActivity" TypeName="*" Authorized="True" />

Creating the .ACTIONS File

The final step is to create the .ACTIONS file that describes the activity to SharePoint Designer. Since this is an XML file, you can create it using Visual Studio or any XML editor. Unfortunately, the XML schema for this file is not part of the SharePoint SDK. So I have created a WSSACTIONS.XSD schema file which you can download here. (Thanks to Scot Hillier for helping to uncover some of the more elusive enumerations like DesignerType and OperatorTypeFrom, etc.) Using the schema file in Visual Studio lets you use intellisense when creating the .ACTIONS file and makes the job of declaring custom activities much easier.

This file describes the public properties exposed by the activity and tells SharePoint Designer how to map those properties into rules that can be displayed to the user. The following code shows a custom .ACTIONS file for the custom EventLogger activity.

<?xml version="1.0" encoding="utf-8" ?>
<WorkflowInfo>
<Actions Sequential="then" Parallel="and">
   <Action Name="Write Message To Event Log"
      ClassName="JohnHolliday.Workflow.EventLogger"
 Assembly="JohnHolliday.Workflow.EventLoggerActivity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0b97b340d4a71524"
      AppliesTo="all" Category="MyCustomActivities">
   <RuleDesigner Sentence="Write '%1' to the event log">
      <FieldBind Field="Message" DesignerType="TextArea" Id="1"/>
   </RuleDesigner>
   <Parameters>
      <Parameter Name="Message" Type="System.String, mscorlib" Direction="In"/>
   </Parameters>
</Action>
</Actions>
</WorkflowInfo>

The Actions tag tells SharePoint Designer what to display for each action in the set. Within that, the Action tag describes the individual action. The Name attribute is what gets displayed in the designer. The ClassName and Assembly attributes are used in the generated XAML for the workflow. The interesting part is the way the RuleDesigner and Parameter tags work. The RuleDesigner tag lets you setup a sentence that gets displayed in the designer as you build up the workflow. The Sentence attribute allows you to bind to the activity properties and then substitute their values when the activity is executed.

Remember the Message property of the custom activity? The FieldBind tag includes a Field attribute that specifies this property by name. The DesignerType attribute specifies which kind of control to use to gather the data, and the Id property specifies the substitution id to use when building the sentence. Now look at the Parameter tag. This specifies the activity side of the contract and is used when calling the Execute method of the activity on the server.

You can declare as many actions as you want in the file. A good rule of thumb is to use a separate .ACTIONS file for each logical group of custom activities you wish to deploy. Once you've created your .ACTIONS file and copied it to the server, you can refresh the site in SharePoint Designer and your custom activity will appear in the workflow designer as shown below.

Summary

Workflow activities are a lot like web parts. The richer your collection of activities, the more powerful workflows you can build. You can leverage the power of Visual Studio to create specialized custom workflow activities and take advantage of the flexibility built into SharePoint Designer 2007 to create declarative workflows that incorporate your custom activities but require no coding. This is a great way to surface customized functionality to a wider audience of administrators, developers and business analysts.

Technorati : , , ,
Del.icio.us : , , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

SharePoint Development | SharePoint Workflow

Comments

3/31/2007 6:36:20 AM

Anonymous

Thanks, this is great - been waiting for an XSD for ages!

Anonymous

4/3/2007 10:47:50 AM

Patrick

Hmm.. I have seen this before. Carlos has posted this three months ago using the same event logger. I think you should credit him for it. He deserves it.

www.ideseg.com/...esToSharePointDesignerPart1.aspx
www.ideseg.com/...esToSharePointDesignerPart2.aspx

Patrick

4/4/2007 6:09:26 AM

Sebastian

Hi,

Thanks for the schema file. I have a problem with it. When I set VS to use it I have to add xmlns="http://schemas.microsoft.com/sharepoint/" to <WorkFlowInfo> root tag of the .actions file for it to work.

But if I add this namespace, my action is not shown in the Designer. If I remove the namespace it is shown back in the Designer.

Could you help?

Sebastian

4/4/2007 10:49:45 PM

Patrick,

Just read the article you referred to - weird how synchronicity works. I don't have a problem giving credit where it is due. Looks like Carlos and I were thinking the same way. Writing to event log is an obvious choice for a simple activity, and avoids unnecessary complexity, since the post is really about customizing SPD and not about writing workflow activities. I'll send a note to Carlos and update the post to point to his entry, especially since he wrote about this first.

Thanks for bringing this to my attention.

John

John Holliday

4/6/2007 5:22:49 AM

Patrick

No problem John. I am actually busy now creating a how-to for the office developer center on the same topic. I'm using another example but both of you (and also Scott Hillier in his book) gave me the foundation for it. You're doing a great job all of you!

Patrick

4/6/2007 5:58:40 AM

Carlos Segura

There's no problem. I have spoken with John and also I think that it's a sheer coincidence.

Carlos Segura

Carlos Segura

4/17/2007 7:21:16 AM

MC

In all the demos I see on the internet people use "base.SetValue", but the snippet's in one of the SDKs use just "SetValue" is there any diference?

MC

5/25/2007 1:38:32 PM

Raghu Iyer

Thanks for the article , though i managed to get the activity in my SPD , but once u select it , it doesnt show uo in the Flow box. ie the sentence is not visible .Nothing happens when i select that activity .Any suggetsions?

Raghu Iyer

5/29/2007 6:38:42 PM

It worked , there was descrepency in the entry between web.config file and .ACTION file.

Raghu Iyer
http://metahat.blogspot.com

Raghu IYer

6/11/2007 5:31:51 AM

Vincenzo

Hello Mr. Raghu IYer,

I have the same problem that you've solved. Could you please explain me which discrepancy you have found? Maybe it's the same than mine... Smile

Thanks for your time!

Vincenzo

6/21/2007 3:10:54 PM

Arpan

Where to deploy the action file?

Arpan

6/23/2007 12:34:56 AM

JFH

Arpan,

You copy the file to 12\TEMPLATE\1033\Workflow\.

JFH

6/29/2007 1:04:22 PM

Amjad

Your excellent post was the first i came across for helping me write custom activities. Every online example shows how to set properties on an activity but I need to write one that will output the results of 2 inputs, for example, i am working on a date diff - provide 2 dates, return the number of days between the 2. This is similar to the Do Calculation activity which takes in 2 parameters and outputs the result to a variable. Any idea how this can be done? I have tried everything but to no avail. I had a look at the Do Calculation action in WSS.ACTIONS for guidance but how do you return a value from code? Any help would be greatly appreciated.

Amjad

7/12/2007 9:16:40 PM

zullu

Nice to see this post.
But I need more heads up!
I understand that I need to create a "Workflow Activity Library" Project to start with.
But what next....where to go....which file to pick and start coding.
Help needed!!!
There is a Program.cs, Workflow1.cs and Workflow1.designer.cs

You have shown some code snippet. where are they suppose to go. Or do I create new items, ......
i have many question !!
But you all experienced guys can definitely guide me through.
I'll appreciate your help.
Thanks,
zullu

zullu

8/29/2007 8:41:29 AM

bszom

Does anyone know where I can find more information about the .ACTIONS file? The XML schema is very helpful already, but I'd like to find out how to expose an enum from within my activity library as a dropdown in the designer, for example.

bszom

9/6/2007 1:44:36 AM

surendra

how to deploy workflow in moss 2007 site
step by step

workflow devlop in sharepoint designer 2007




surendra

9/19/2007 7:45:49 PM

mehmetk

fine thanks..

http://www.kliplive.net" title="kliplive">klip

mehmetk

10/11/2007 1:51:33 AM

MAC

For Vincenzo,

I had the same problem that yourself and M. Raghu IYer have experienced. My problem turned out to be that I had '_' characters in the assembly name. By chance, I replaced all occurrences of '_' (namespace, assembly, etc) with '.' and it fixed my problem.

MAC

1/20/2008 10:17:01 PM

arc

The activity shows up in Designer Workflow but the code does not seems to be doing anthing, nothing gets added to the Event Log?

arc

2/22/2008 7:12:34 AM

Anil

Hi i want to creat a custom work flow......it goes like .......when ever a Doc is Created it has to be sent to a gRoup of Checkers. If one of the checkers approve the doc than it has to go to a Group of Approvers and if one of the approver approves it The work flow is completed. But on the other hand , if one of the checker or approver rejects the doc it goes back to the originator.

Anil

3/15/2008 6:21:41 PM

wijnand

it doesnt show uo in the Flow box. ie the sentence is not visible .Nothing happens when i select that activity .Any suggetsions?

wijnand

5/11/2008 12:09:37 AM

Eric Wang

me too, any one can help?
Thanks!

Eric Wang

6/26/2008 5:54:36 PM

Martin

Hi, if a choose my activity in designer, the sentence is not visible.. Nothing is happend, when i press a validation button it says workflow is ok. I think, this solution is partially good, it doesn't solve all problems with creating and showing activity in Sharepoint Designer.

Martin

7/6/2008 12:20:37 PM

alex

I have same problem - "if a choose my activity in designer, the sentence is not visible"

alex

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Copyright © 2005-2008, John F. Holliday
This work is licensed under a Creative Commons License Powered by BlogEngine.NET 1.4.0.0

About Me

John Holliday

Independent author, consultant, trainer, and software developer specializing in enterprise content management, collaboration, workflow and business process automation. SharePoint training for developers and administrators

 

Recent comments

Comment RSS