Format a date in a SharePoint Designer workflow using a custom action

Learn how you can create a custom SharePoint Designer 2007 workflow action that accepts a date and a date format and returns a formatted date as a variable that you can use in the SharePoint Designer workflow.

ADVERTISEMENTS

As you may already know, SharePoint Designer 2007 does not offer an out-of-the-box workflow action to be able to format dates.

However, you can follow the instructions given in Building Custom Activities for Use in SharePoint Designer 2007 to create a custom workflow action that can be used in SharePoint Designer 2007 to format dates.

Create the Workflow Activity Library project

  1. In Visual Studio 2005, create a new Workflow Activity Library project. Name the project DocumentManagementWorkflowInfoActivityLib.
  2. Rename the Activity1.cs file to FormatDateActivity.cs.
  3. Add the following DependencyProperties in the FormatDateActivity class:

    public static DependencyProperty DateFormatProperty =
    DependencyProperty.Register(
    "DateFormat", typeof(string), typeof(FormatDateActivity));
    public static DependencyProperty DateToFormatProperty =
    DependencyProperty.Register(
    "DateToFormat", typeof(DateTime), typeof(FormatDateActivity));
    public static DependencyProperty FormattedDateProperty =
    DependencyProperty.Register(
    "FormattedDate", typeof(string), typeof(FormatDateActivity));

  4. Add the following Properties to the FormatDateActivity class:

    [Category("DocumentManagementWorkflowInfo Actions"), Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string DateFormat
    {
    get
    {
    return ((string)(base.GetValue(
    FormatDateActivity.DateFormatProperty)));
    }
    set
    {
    base.SetValue(FormatDateActivity.DateFormatProperty,
    value);
    }
    }

    [Category("DocumentManagementWorkflowInfo Actions"), Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public DateTime DateToFormat
    {
    get
    {
    return ((DateTime)(base.GetValue(
    FormatDateActivity.DateToFormatProperty)));
    }
    set
    {
    base.SetValue(FormatDateActivity.DateToFormatProperty,
    value);
    }
    }

    [Category("DocumentManagementWorkflowInfo Actions"), Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string FormattedDate
    {
    get
    {
    return ((string)(base.GetValue(
    FormatDateActivity.FormattedDateProperty)));
    }
    set
    {
    base.SetValue(FormatDateActivity.FormattedDateProperty,
    value);
    }
    }

    This code defines 3 properties: A DateFormat property in which the date format must be specified, a DateToFormat property in which the date to be formatted must be specified, and a FormattedDate property in which the resulting formatted date will be stored when the activity is executed.

  5. And finally, add the following Execute method to the FormatDateActivity class:

    protected override ActivityExecutionStatus Execute(
    ActivityExecutionContext executionContext)
    {
    try
    {
    this.FormattedDate =
    this.DateToFormat.ToString(this.DateFormat);
    }
    catch (Exception ex)
    {
    System.Diagnostics.EventLog.WriteEntry(
    "DMWI Custom SPD Activity: ", ex.ToString());
    }
    return ActivityExecutionStatus.Closed;
    }

  6. Sign the project.
  7. Save and build the project.
  8. Deploy the assembly to the GAC.

Create the .ACTIONS file

  1. Create a new text file named DMWI.ACTIONS in SharePoint’s \12\TEMPLATE\1033\Workflow directory.
  2. Open the file in Notepad and add the following XML code to the file:

    <?xml version="1.0" encoding="utf-8"?>
    <WorkflowInfo Language="en-us">
    <Actions Sequential="then" Parallel="and">
    <Action Name="Format a Date"
    ClassName="DocumentManagementWorkflowInfoActivityLib.FormatDateActivity"
    Assembly="DocumentManagementWorkflowInfoActivityLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7f8c689bf4639afb"
    AppliesTo="all" Category="DocumentManagementWorkflowInfo.com Actions">
    <RuleDesigner
    Sentence="Format %1 as %2 (Output to: %3)">
    <FieldBind Field="DateToFormat" DesignerType="Date" Text="date" Id="1"/>
    <FieldBind Field="DateFormat" DesignerType="TextArea" Text="format" Id="2"/>
    <FieldBind Field="FormattedDate" DesignerType="ParameterNames" Text="formatted date" Id="3"/>
    </RuleDesigner>
    <Parameters>
    <Parameter Name="DateToFormat" Type="System.DateTime, mscorlib" Direction="In" />
    <Parameter Name="DateFormat" Type="System.String, mscorlib" Direction="In" />
    <Parameter Name="FormattedDate" Type="System.String, mscorlib" Direction="Out" />
    </Parameters>
    </Action>
    </Actions>
    </WorkflowInfo>

    Note: You will have to get the PublicKeyToken for your own assembly from the GAC.

  3. Save and close the file.

Authorize the assembly for the custom activity

  1. Open the web.config file for your SharePoint web application. In a default Windows SharePoint Services (WSS) 3.0 installation this file is located in the Inetpub\wwwroot\wss\VirtualDirectories\80 directory.
  2. Add the following code to the <authorizedTypes> section of the SharePoint web application’s web.config file:
    <authorizedType Assembly="DocumentManagementWorkflowInfoActivityLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7f8c689bf4639afb" Namespace="DocumentManagementWorkflowInfoActivityLib" TypeName="*" Authorized="True" />
  3. Save the web.config file.

Use the custom action in SharePoint Designer and test it

After you deploy and authorize the custom SharePoint Designer activity, you can use it as a workflow action in SharePoint Designer 2007 just like any other out-of-the-box action.

I’ve created a demo on how to use this custom SharePoint Designer workflow activity to format a date of a SharePoint list item with the format ‘dd MMMM yyyy’.

Related Articles

ADVERTISEMENTS