4 minute read

When you create a SharePoint approval workflow the While activity is often used to check if a Task is approved by placing the “OnTaskChanged” activity within the While Activity.

workflow

When you also want to check if the Moderation status on the workflow item is approved you can’t do this in the same While activity.

If you only check the task status there will be situation in which the item is approved and the approval workflow is still running because the item was approved directly. If you would like to check de moderation status of an item and the task status you can use a “ConditionedActivityGroup”.

The “ConditionedActivityGroup” will cause the workflow to continue when the moderation status is changed or when the status of the task is changed. To use the “ConditionedActivityGroup” drag the activity within the workflow designer.

Workflow_ConditionedActivityGroup

Next drop the activities you want to check within the small window at the top that states “Drop Activities Here”. For our example we will use the “OnTaskChanged” and the “OnWorkflowItemChanged” activities.

Workflow_ConditionedActivityGroup_Activities_2

Open the workflow in code view and add three boolean properties:

  • IsFinished
  • EnableItemChanged
  • EnableTaskChanged
public bool IsFinished { get; set; }

public bool EnableItemChanged { get; set; }

public bool EnableTaskChanged { get; set; }

These properties will be used to activate the conditions within the condition group. The “IsFinshed” property will be used to finish the conditioned group. To activate the conditions we will also have to set the “EnableItemChanged” and “EnableTaskChanged” properties to true within the initialization of the workflow.

/// <summary>
/// Initializes a new instance of the worfklow.
/// </summary>
public Workflow1() {
    InitializeComponent();
    this.EnableItemChanged = true;
    this.EnableTaskChanged = true;
}

 

After writing those lines of code open the workflow designer and select the “ConditionedActivityGroup” and change the “UntilCondition” property of the activity to: Declarative Rule Condition.

Select the “…” at the end of the “ConditionName” property and create a new “Condition”. The value of the condition has to be “this.IsFinished”.

Condition

This will mean that the ConditionedActivityGroup is finished when “IsFinished” equals true.

The next step is to set the “WhenCondition” on the activities that are in the group activity. These properties we also be a “Declarative Rule Condition” and set the condition values to “this.EnableTaskChanged” for the task activity and “this.EnableItemChanged” for the item activity.

Besides this we will also set the “Invoked” property of the activities to perform a code action when the event take place (You can also let Visual Studio do this by double clicking the activities).

 

WhenCondition

 

With all this in place when can start to write some code for the invoke methods. Because the “WhenCondition” mean that when they are set to true these conditions can occur. We will have to set these properties to true each time a Condition is met because the Workflow instance will automatically set them to false. In our example this will occur when the task status is set to approved or the item is approved.

public const string FieldId = "YourFieldID";

/// <summary>
/// Tasks the item changed.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Workflow.Activities.ExternalDataEventArgs"/> instance containing the event data.</param>
private void TaskItemChanged(object sender, ExternalDataEventArgs e) {
    //retrieve the task and check its status
    SPListItem task = workflowProperties.TaskList.GetItemById(CreateTaskActivity.ListItemId);
    string status = task[YourFieldID] != null ? task[YourFieldID].ToString() : string.Empty;

    if (status.ToUpperInvariant() != "PENDING") {
        IsFinished = true;
        EnableTaskChanged = false;
    } else {
        IsFinished = false;
        EnableTaskChanged = true;
    }
}

/// <summary>
/// Workflows the item changed.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Workflow.Activities.ExternalDataEventArgs"/> instance containing the event data.</param>
private void WorkflowItemChanged(object sender, ExternalDataEventArgs e) {
    //View the moderation status of the workflow item
    SPModerationStatusType type = workflowProperties.Item.ModerationInformation.Status;
    Status = type.ToString();
    if (type == SPModerationStatusType.Approved || type == SPModerationStatusType.Denied) {
        IsFinished = true;
        EnableItemChanged = false;
    } else {
        IsFinished = false;
        EnableItemChanged = true;
    }