Creating custom Workflow for Umbraco tasks

This post details how to create custom tasks for use with Workflow for Umbraco. If you haven't already done so you may benefit from watching the introductory screencast in this blog post.

In the screencast below I run through how to create your own workflow tasks and introduce some features that I didn't get around to in the first screen cast.

The code demonstrated in the screencast is available for download here. If you create and send me a sample workflow task for our library of samples then i'll give you a free worklow license (small print below).

Tasks2

The key points of the screencast are:

[Serializable]
public class BitLyUrlShorteningTask : BaseWorkflowTask, IRunnableWorkflowTask
{
[Serializable]
public BitLyUrlShorteningTask() : base()
{
    // A workflow task should let the designer know what possible outcomes
    AvailableTransitions.Add("done");
}
[Serializable]
// Implement the Run method of IRunnableWorkflowTask
public void Run(IWorkflowInstance workflowInstance, IWorkflowRuntime runtime)
{
    // In Umbraco the workflowInstance should always be castable to an UmbracoWorkflowInstance
    var wf = (UmbracoWorkflowInstance) workflowInstance;
    ....
runtime.Transition(workflowInstance, this, "done");
// An Entity Ui describes how the workflow task is rendered in the workflow Designer
public class BitLyUrlShorteningTaskEntityUi : BaseWorkflowTaskEntityUi, IWorkflowTaskEntityUi, IGlobalisable
{
    ...
// An Entity Ui describes how the workflow task is rendered in the workflow Designer
public BitLyUrlShorteningTaskEntityUi()
            : base()
{
    // Describe the workflow transition
    TransitionDescriptions.Add("done", "URL Was shortened");

    // Decorate your task with a custom CSS class in the designer.
    // The workflow designer scans ~/umbraco/plugins/fmworkflow/css for custom CSS files and includes all of them.
    UiAttributes.Add("class", "bitlyTask");
            
    // These properties explain how to present a Ui to get values for the public properties of the workflow task.
    UiProperties.Add(new BitLyApiKeyPropertyUi());
    UiProperties.Add(new BitLyLoginPropertyUi());
    UiProperties.Add(new DocumentTypePropertyUi());
}
// When passed an object returns a bool indicating whether this task can supply a UI for it.
public override bool SupportsType(object o)
{
    return o.GetType() == typeof(BitLyUrlShorteningTask);
}

// Name of task
public override string EntityName
{
    get { return "Bit.ly URL Shorten"; }
}
public class BitLyApiKeyPropertyUi : PropertyUi, IWorkflowUiProperty, IGlobalisable
{
    ....
public Control RenderControl { get; private set; }

public BitLyApiKeyPropertyUi()
{
    // Just says that a TextBox will be used to get the property value from the user.
    RenderControl = new TextBox { ID = PropertyName, CssClass = "workflowTextBox" };
}
// The workflow designer uses this getter and setter to give or take the value of the property.
public object Value
{
    get { return ((TextBox)RenderControl).Text; }
    set { ((TextBox)RenderControl).Text = (string)value; }
}
// Label for control when rendered in Ui
public string Label
{
    get { return "Bit.ly API key"; }
}

// The workflow designer uses this getter and setter to give or take the value of the property.
public object Value
{
    get { return ((TextBox)RenderControl).Text; }
    set { ((TextBox)RenderControl).Text = (string)value; }
}

If you send me a sample worklflow task I'll add it to our library and generate you a free license. It needs to be a sample that I don't have already and do something useful (not just log Hello world to a file etc). I'll be the final just of what is useful :)

Leave a comment