Handling tasks in a workflow

Through this blogpost I’m extending my original workflow project with some additional branching by demonstrating the basic concepts of authoring workflows for MOSS 2007 using the templates provided by Microsoft.
This post demonstrates how and where you would write your code, drop your activities.
Designing our workflow
Lets say I roll a dice. Great! If I get anything over 3, A task and email notification is initiated to notify the originator that they’ve won a prize. Although this is purely an example, this type of functionality could be very useful.
1)    Add an ifElse activity, this should automatically add at least on ifElseBranch activities.
2)    Drag a CreateTask activity under the branch activity. 
Your workflow should now look like this…

Write the code for the WF
Excellent. Now lets get rid of those checkboxes, and write the code for the various activities we dropped.
First, the rolled dice will now have to be accessible all through the workflow. So go ahead and modify the codeActivity1_ExecuteCode method to as below –
private Int32 diceValue= 0;
 
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
    Random rnd = new Random() ;
   diceValue = rnd.Next(1, 6);
   workflowProperties.Item[“”Title””] = diceValue;
   workflowProperties.Item.Update();
}
Now that the diceValue is a private variable, go ahead and hover over the red checkmark at the IfElseBranchActivity. It should tell you that the Condition is not set. Go ahead and click on it, and set a “”Declarative Rule Condition”” (simpler) inside of the properties dialog for the IfElseBranchActivity. Expand that tree view for “”Condition””, and click the ellipses by the “”Condition Expression””, and specify a condition as shown below:

You would note that you have full intellisense here. very nice!
The next step is to give flesh to the bones of the createTask1 activity. This is a tad bit tricky. Just tricky, not scary.
First of all, when you hover over the red checkmark by createTaskActivity, it tells you that correlationToken isn’t set. CorrelationTokens are an interesting animal.
So in our case, we have a createTask activitiy, which means, we need to create a NEW correlation token, that applies to Tasks. This ensures that subsequent activities can find that task and act upon it. In order to do so, go to the properties of the createTask1 Activity, and set the correlation token to “”taskToken””.DoNot pick workflowToken as prompted by the drop down. Also set the OwnerActivityName to any parent – I choose Workflow1 (whoaa, a workflow is an activity? yep!).
The next thing to do with createTaskActivity is frankly a tad bit annoying. Double click on the createTaskActivity and it will create a createTask1_MethodInvoking for you. It will also set the createTask1.MethodInvoking property appropriately.
Now what we need to do in the MethodInvoking property, is to set the TaskID (which is a GUID), and TaskProperties. In order to do so, first you need to create two new Fields (not DependencyProperties), that you will use for TaskID and TaskProperties. You can use the following steps to acheive this.
1. Back in WF Designer, right click, properties on the createTask1 activity.
2. In the Properties, click on the ellipsis by TaskID, and go to he Bind to a New Member Tab. Fill in the form as shown below.

3. Repeat the same for TaskProperties, make sure it is bound to a new Field called “”createTask1_TaskProperties1″”
Perfect. Now with these two fields setup, go ahead and modify the code for createTask1_MethodInvoking to as below:
private void createTask1_MethodInvoking(object sender, EventArgs e)
{
   createTask1_TaskId1 = Guid.NewGuid();
   createTask1_TaskProperties1.AssignedTo = workflowProperties.Originator;
   createTask1_TaskProperties1.Title = “”Congratulations, thou art ye winner!””;
   createTask1_TaskProperties1.Description = “”You have won!!! Now go and claim your prize””;
   createTask1_TaskProperties1.SendEmailNotification = true;
}
 
As you can see, I am using the createTask  Activity to send the email to the user. So I don’t need to worry about a sendEmail activity.
So, That’s it, your Workflow1.cs class code should look something like this… 
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
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;
using System.Xml.Serialization;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.Office.Workflow.Utility;
namespace CopyAbstractContent
{
 public sealed partial class Workflow1: SharePointSequentialWorkflowActivity
 {
  public Workflow1()
  {
   InitializeComponent();
  }
        public Guid workflowId = default(System.Guid);
        public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties workflowProperties = new Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();
        private Int32 diceValue = 0;
        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            Random rnd = new Random();
            diceValue = rnd.Next(1, 6);
            workflowProperties.Item[“”Title””] = diceValue;
            workflowProperties.Item.Update();
        }
        private void createTask1_MethodInvoking(object sender, EventArgs e)
        {
            createTask1_TaskId1 = Guid.NewGuid();
            createTask1_TaskProperties1.AssignedTo = workflowProperties.Originator;
            createTask1_TaskProperties1.Title = “”Congratulations, thou art ye winner!””;
            createTask1_TaskProperties1.Description = “”You have won!!!! Now go and claim your prize””;
            createTask1_TaskProperties1.SendEmailNotification = true;
        }
        public Guid createTask1_TaskId1 = default(System.Guid);
        public SPWorkflowTaskProperties createTask1_TaskProperties1 = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();

         }
}
 
Testing the workflow
Now, I have already covered Deploy, Run, Debug, in my previous blogpost, so I won’t waste too much breath on that here. So let us dive straight into “”Enjoy””.
1. Create a new list.
2. Go to List settings, and associate an instance of the Roll Of Dice workflow to that list.
3. Add an item in there
4. Run the workflow you created in #2 above.

Now when I “”win””, i.e. get a value > 3, here is what I get for my Task.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s