Skip to main content

Adding Dynamic Activities

If you want to simplify your workflows and improve their speed, you can use code to add dynamic activities to your workflows at run time. This tutorial demonstrates how to create two forms and a workflow, add one form to the workflow dynamically, add the other form statically, and pass values from the dynamic form to the static one in code.

What You'll Learn

In this tutorial, you will:

  • Create a dynamic form activity with editable fields
  • Create a static form activity with read-only fields
  • Add a dynamic activity to a workflow at runtime using SystemCodeActivity
  • Add assembly references for dynamic forms
  • Create event handlers for DynamicCreated and DynamicClosed events
  • Pass data from dynamic forms to static forms
  • Compile and publish workflows with dynamic activities

Prerequisites

caution

This tutorial assumes you are familiar with basic workflow creation in Workflow Studio. You should understand how to create forms and basic C# programming concepts.

Create Dynamic Form Activity

We will add this form activity to the workflow dynamically.

  1. In Workflow Studio, in the Solution Explorer, right-click the package in which to create the form, expand Add New Item, then User Interface, and select EmpowerID Form.
    Add new EmpowerID Form

  2. In the New EmpowerID Form wizard that appears, click Next.
    Form wizard next button

  3. Enter a Name for your form and click Next. The name value is how you reference the form in code.
    Name dynamic form

  4. Enter a Title and Description for the form and click Next. The title and description values appear on the form at run time.
    Title and description for dynamic form

  5. Select the User Interface Forms group in which to place the form within the package and click Next.
    Group selection for dynamic form

  6. A blank form appears. On the tool ribbon, click Add Object.
    Add object button

    info

    To preserve screen space, you can hide the left and right panels using the green arrows in the toolbar just above the document space. You can hide the ribbon along the top by double-clicking the current ribbon tab.

  7. In the Add Object dialog that appears, select String and click Add.
    Add string object

  8. In the Components pane to the left of the form, String1 appears under Other Objects.
    String1 component

  9. Double-click String1 and change the text to FirstName, then drag it onto the form. Double-click FirstName on the form to add a space between words.
    FirstName field on form

  10. To add a second field to the form, on the tool ribbon, click Add Object and this time, select Email as the type of object to add.
    Add email object

  11. In the Components panel, double-click Email1 and change the text to EmailAddress, then drag it onto the form.

  12. Click Save, then Compile and Publish the form. You can restart services and restart Workflow Services at this time.
    Compile dynamic form

Create Static Form Activity

Here, we essentially create the same form as above, but this time with read-only fields.

  1. Create a new form as above, but with a different Name, and click Next.
    Name static form
  2. Provide a Title and Description for the form and click Next.
    Title and description for static form
  3. For the group name, select User Interface Forms and click Next.
    Group selection
  4. A blank form appears. On the tool ribbon, click Add Object, select String, and click Add.
    Add string object
  5. Double-click String1 and change the text to FirstName, then drag it onto the form. Double-click FirstName on the form to add a space between words.
    FirstName field
  6. Right-click First Name on the form and select Edit.
    Edit field context menu
  7. In the Field Configuration window that appears, select Read Only and click OK.
    Set field to read only
  8. To add a second field to the form, on the tool ribbon, click Add Object and this time, select Email as the type of object to add.
    Add email object
  9. In the Components panel, double-click Email1 and change the text to EmailAddress, then drag it onto the form.
  10. Right-click and select Edit, then select Read Only and then OK.
  11. Click Save, then Compile and Publish the form, this time restarting your Web Role Service and restarting Workflow Studio.
caution

It is important to restart Workflow Studio at this point. Until you do so, the forms are not available for use in workflows.

Create Workflow with Dynamic Activities

In this workflow, we use a system code activity to add an instance of the dynamic form, and add a reference to the form so that the workflow has access to its properties.

  1. In Workflow Studio, in the Solution Explorer, right-click the package in which to create the workflow, expand Add New Item, then Workflow Application, and select Flow Chart Workflow.
    Create flow chart workflow

  2. In the Save Workflow dialog that appears, the Package is populated with the one you selected. Fill in the Name, Workflow Title, and Description fields as appropriate, and click OK.
    Save workflow dialog A blank workflow with only FlowStart and FlowStop endpoints appears on the Workflow Designer tab.
    Blank workflow

  3. In order to add an activity dynamically, we must add a reference to the activity's assembly, in this case, MyDynamicForm.

    • In the right panel, click the Code Tree tab, right-click the References node, and select Add Reference, then Add Assembly Reference.
      Add assembly reference
    • In the GAC Assembly Reference dialog that appears, type the first letter of your dynamic form's name, then select it and click Add selected items.
      GAC assembly reference dialog
  4. In order to add the code to call and act upon a dynamic activity, we must add a System Code Activity to the workflow.

    • In the right panel, click the Activities tab and search for SystemCodeActivity, then drag the activity onto the workflow.
      System code activity

    • In the Properties tab, under Activity, set the Name value, in this example MyDynamicActivity.

    • Double-click MyDynamicActivity. This creates a handler where you can enter code to execute for the activity.
      Dynamic activity event handler

    • Add code like the following to the event handler, where MyDynamicFormActivity is the full name of the class we created.
      Class reference

      // Add an instance of the dynamic form to this activity.
      MyDynamicFormActivity dynamicForm = new MyDynamicFormActivity();
      this.MyDynamicActivity.AddDynamicActivity(dynamicForm);
  5. In order to pass values into the dynamic activity on creation, we must create an event handler for the DynamicCreated event where we can add code.

    • With the System Code Activity selected (DynamicActivity in this example), in the Properties tab under Dynamic Activity, type a name for your event handler in the DynamicCreated property. In this example, we use OnDynamicFormCreate.
      Dynamic created property

    • Press Enter and the code editor appears below the workflow.

      info

      If you want to open the code editor for the event handler later, click the Solution tab and expand the Event Handlers node, then double-click the event handler.

      Event handler in solution tree

    • Add code like the following to pre-populate the form with values.

      // Get a known dynamic activity.
      MyDynamicFormActivity dynamicForm = e.GetActivity<MyDynamicFormActivity>();
      // Another way to get the dynamic activity.
      MyDynamicFormActivity dynamicForm2 = e.Activity as MyDynamicFormActivity;
      // Pre-fill the form with values.
      dynamicForm.FirstName = "Enter your first name here";
      dynamicForm.EmailAddress = "enteryour@email.com";
  6. To add the static form to the workflow, on the Activities tab, search for the static form you created, MyStaticFormActivity in this example, and drag the activity onto the workflow.
    Add static form to workflow

  7. To pass values from the dynamic form to the static form, you must create an event handler for the DynamicClosed event.

    • With the System Code Activity selected (DynamicActivity in this example), in the Properties tab under Dynamic Activity, type a name for your event handler in the DynamicClosed property. In this example, we use OnDynamicFormClose.

    • Press Enter and the code editor appears below the workflow.

    • Add code like the following to pass values from the dynamic form to the static form.

      // Get a known dynamic activity.
      MyDynamicFormActivity dynamicForm = e.GetActivity<MyDynamicFormActivity>();
      // Another way to get the dynamic activity.
      MyDynamicFormActivity dynamicForm2 = e.Activity as MyDynamicFormActivity;
      // Pass values from the dynamic form to the static form.
      this.MyStaticForm.FirstName = dynamicForm.FirstName;
      this.MyStaticForm.EmailAddress = dynamicForm.EmailAddress;
  8. The last step in creating a dynamic activity is to compile and publish the workflow.

Compile and Publish Workflow

  1. In the toolbar above the document space, click the Save icon, then click the Compile and Publish icon.
    Compile and publish workflow
  2. In the Publish Workflow wizard that appears, click Next, select the Host, then click Next again.
    Publish wizard next
  3. Once compilation is complete, a Request Workflow message box appears. Click Yes to create a new request workflow.
    Request workflow prompt
  4. In the Add Request Workflow dialog that appears, you can change the Name, Friendly Name, Description, and URL values, and specify whether to Allow Anonymous Access. Click OK to create the request workflow. Make note of the URL value.
    Add request workflow dialog
  5. In the Restart Service message box that appears, click Yes.
  6. In the Active Services dialog that appears, expand your server and select the EmpowerID Web Role Service, and at the bottom, select the Reset IIS checkbox and click Restart.
    Active services dialog

Verify Workflow is Working

  1. Log into your EmpowerID web application.
  2. Navigate to your workflow by appending the URL value from the request workflow to your EmpowerID Web Server address in the following format:
    https://<YourEmpowerIDServer>/ui/#w/MyDynamicActivityWorkflow

Replace <YourEmpowerIDServer> with your actual server address (for example, sso.empoweriam.com). 3. The Dynamic Form form activity appears, pre-populated with values.
Dynamic form at runtime 4. Enter new values and click Submit. The static form appears, populated with the values passed from the dynamic form.