- Create a Workflow Project (part 01)
- Create Custom Task List (part 02)
- Create a Custom InfoPath form (part 03)
- Create a Workflow utilizing the CreateTaskWithContentType (part 04)
- Set up your workflow to work (part 05)
Let's Make a Workflow
1.) Open up that workflow you created in the very first post, and let's have a lookRight after the onWorkFlowActivated activity add the following:
- LogToHistoryListActivity
- CreateTaskWithContentType
- create a WhileActivity
- Inside the while activity add a onTaskChanged activity
- right outside the while activity add a complete task activity
- finally create another Log To History Activity
createTaskWithContentType1
Lets get started, click on the createTaskWithContentType1 activity and hit the F4 key to bring up the properties.Create a correlationToken, type in the value TaskToken and pick Workflow1 as the OwnerActivity Name. That should get rid of your Exclamation mark, but we're not done we're going to have to do some binding.
First bind the ContentTypeId to a custom field, how you ask?
click on the property to give it focus, this should bring up three little dots on the left of the value field, click that
this will bring up the "Bind 'Content TypeId' to an Activity's Property" dialogue box, select the "Bind to a new member" tab, Change the name to something meaningful, switch the type to Create Field or don't, it doesn't really matter.
with that complete hit OK, you have two more properties to bind, follow the same procedure as above
- For the TaskId property, I set the field name to Task_Id
- For the TaskProperties property, I set the field name to Task_Properties
Now for the MethodInvoking property just type in createTask and hit enter, that should bring you to the bound function createTask, here you're going to set your task properties, the value for your task id field.
the three task properties you're going to set are:
- Assigned to - who does the task
- Title - what the task is called
- TaskType - which task form to use (it'll become clearer later on)
so here's what you're function is suppose to look like when complete as well as the fields you created
public String Task_ContentTypeId = default(System.String); public Guid Task_Id = default(System.Guid); public SPWorkflowTaskProperties Task_Properties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties(); private void CreateTask(object sender, EventArgs e) { Task_Id = Guid.NewGuid(); Task_Properties.AssignedTo = @"domain\SPtest3"; Task_Properties.Title = "Magical Task"; Task_Properties.TaskType = 0; }
and here's what the properties of your activity should be
Next we're going to tackle the onTaskChanged activity
Set your CorrelationToken property to the same one as for createTaskWithContentType1, TaskToken.When it comes to binding your TaskId property, do NOT create a new field, bind to the existing one from before.
Next you have to bind the BeforeProperties property to a new field, I call it Task_beforeProperties, it's good practice to bind the AfterProperties as well, but not necessary for an example this simplistic.
With your on task changed activity wrapped up lets focus on that while loop.
Click on your while loop, hit F4 to see the properties, change the Condition property to Code Condition and call the Condition CheckText and hit enter.once you hit enter you should see the function in the code behind, now this function has a ConditionalEventArgs that can be set to a boolean value representing if it should loop or not.
- True = loop
- False = exit loop
private void CheckText(object sender, ConditionalEventArgs e) { e.Result = Task_BeforeProperties.ExtendedProperties["KiwiMango"] == null ? true : string.IsNullOrEmpty(Task_BeforeProperties.ExtendedProperties["KiwiMango"].ToString()); }
Or if you prefer
private void CheckText(object sender, ConditionalEventArgs e) { if (Task_BeforeProperties.ExtendedProperties["KiwiMango"] == null) { e.Result = true; } else if (Task_BeforeProperties.ExtendedProperties["KiwiMango"].ToString() == string.Empty) { e.Result = true; } else { e.Result = false; } }
We're going to set up our complete task.
- Again set the correlation Token to TaskToken
- Bind the task Id like you did before
- Now you could set your TaskOutcome property to static text such as Task complete, but let's do something more interesting. instead in the methodInvoking property type in completeTask and hit enter; this should bring you back to your code behind an here we'll set the task outcome to include our value from our form.
private void completeTask(object sender, EventArgs e) { ((CompleteTask)sender).TaskOutcome = string.Format("KiwiMango = {0}", task_BeforeProperties.ExtendedProperties["KiwiMango"].ToString()); }
now what you've done is cast the sender argument to a CompleteTask activity and then set the taskOutcome property in code to include the parameter passed back from the user form.
Log activities
To wrap this post up lets set up our two log activities, so that we can know what on earth is going on, the first one lets set it to let us know that:- the work flow has started
- we're about to create a task
- the task is complete
- workflow is complete
the code for the first log is
private void logToHistoryListActivity1_MethodInvoking(object sender, EventArgs e) { ((LogToHistoryListActivity)sender).HistoryDescription = "workflow initiated"; ((LogToHistoryListActivity)sender).HistoryOutcome = "about to create task"; }
and for the second log is
private void logToHistoryListActivity2_MethodInvoking(object sender, EventArgs e) { ((LogToHistoryListActivity)sender).HistoryDescription = "Task Complete"; ((LogToHistoryListActivity)sender).HistoryOutcome = "about to Complete workflow"; }
and there you go, if you deploy and attach your workflow it's going to fail, but that's cause we're not done yet; it's now time for the real pain in the ass part, making the damn thing work. but that's in the next post.
continued