Thursday, August 20, 2009

How to create a SharePoint State Machine Workflow: Part 6 - Add task notification emails

Here in Part 6, we'll add email notification events so when a task is assigned, approved, or rejected, the appropriate emails are sent to the proper people. This part assumes you have completed part 4.

1. go back to the design surface and double-click PeerReviewerApprovalInitialization to drill down into it

2. drag and drop a SendEmail activity below hlogPeerReviewerApprovalTaskCreated
and set the following properties:
a. Name: sendPeerReviewerApprovalTaskEmail
b. Body: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
c. CC: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
d. CorrelationToken: choose workflowToken from dropdownlist
e. Subject: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
f. To: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
g. MethodInvoking: type PeerReviewerApprovalTaskEmail and hit enter
h. You will be taken to the code stub for PeerReviewerApprovalTaskEmail. Copy and paste the following code:

'get PeerReviewer's email address
Dim PeerReviewerObject As SPUser = GetUserObject(PeerReviewer)
Dim PeerReviewerEmail = PeerReviewerObject.Email

'get CC's email address
Dim CCObject As SPUser = GetUserObject(CC)
Dim CCEmail = CCObject.Email


sendPeerReviewerApprovalTaskEmail_To = PeerReviewerEmail
sendPeerReviewerApprovalTaskEmail_CC = CCEmail
sendPeerReviewerApprovalTaskEmail_Subject = "Approval of " & workflowProperties.Item.File.Name & " has been assigned to you."
sendPeerReviewerApprovalTaskEmail_Body = "<span style='font-family: arial; font-size: medium'>" & _
"Task assigned by " & workflowProperties.OriginatorUser.Name & " on " & DateTime.Now & _
"</span>" & _
"<br>" & _
"<span style='font-family: arial; font-size: x-small'>" & _
"Due by " & DateTime.Today & _
"<br><br>" & _
"Instructions: <br>" & Instructions & _
"<br><br>" & _
"Please approve " & workflowProperties.Item.File.Name & _
"<br><br>" & _
"To complete this task:" & _
"<br><br>" & _
"    1. Review " & "<a href='" & workflowProperties.SiteUrl & "\" & workflowProperties.ItemUrl & "'>" & workflowProperties.Item.File.Name & "</a>" & "." & _
"<br>" & _
"    2. Use the <b>Edit this task</b> button to Approve/Reject the document." & _
"<br><br>" & _
"To view this workflow's history, click " & "<a href=" & workflowProperties.SiteUrl & "/_layouts/WrkStat.aspx?List=" & workflowProperties.ListId.ToString & "&WorkflowInstanceID=" & workflowProperties.WorkflowId.ToString & ">here</a>" & "." & _
"</span>"

'if this is the first time we have gone to the peer reviewer state, do not show comments
' in email
If ArrivedFromInitiatorState = True Then
'get the comments entered by the initiator
Dim Comments As String = onInitiatorApprovalChanged.AfterProperties.ExtendedProperties("txtComments").ToString

sendPeerReviewerApprovalTaskEmail_Body = sendPeerReviewerApprovalTaskEmail_Body & _
"<span style='font-family: arial; font-size: x-small'>" & _
"<br><br>" & _
"<font color=red>Comments:</font> <br>" & Comments & _
"</span>"
End If

3. go back to the design surface and double-click InitiatorApprovalInitialization to drill down into it

4. drag and drop a SendEmail activity below hlogPeerReviewerApprovalTaskCreated
and set the following properties:
a. Name: sendInitiatorApprovalTaskEmail
b. Body: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
c. CC: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
d. CorrelationToken: choose workflowToken from dropdownlist
e. Subject: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
f. To: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
g. MethodInvoking: type InitiatorApprovalTaskEmail and hit enter
h. You will be taken to the code stub for PeerReviewerApprovalTaskEmail. Copy and paste the following code:

'get Initiator's email address
Dim InitiatorObject As SPUser = GetUserObject(workflowProperties.Originator)
Dim InitiatorEmail = InitiatorObject.Email

'get CC's email address
Dim CCObject As SPUser = GetUserObject(CC)
Dim CCEmail = CCObject.Email

'get PeerReviewer's name
Dim PeerReviewerObject As SPUser = GetUserObject(PeerReviewer)
Dim PeerReviewerName = PeerReviewerObject.Name

'get the comments entered by the peer reviewer
Dim Comments As String = onPeerReviewerApprovalChanged.AfterProperties.ExtendedProperties("txtComments").ToString

sendInitiatorApprovalTaskEmail_To = InitiatorEmail
sendInitiatorApprovalTaskEmail_CC = CCEmail
sendInitiatorApprovalTaskEmail_Subject = "Approval of " & workflowProperties.Item.File.Name & " has been assigned to you."
sendInitiatorApprovalTaskEmail_Body = "<span style='font-family: arial; font-size: medium'>" & _
"Task assigned by " & PeerReviewerName & " on " & DateTime.Now & _
"</span>" & _
"<br>" & _
"<span style='font-family: arial; font-size: x-small'>" & _
"Due by " & DateTime.Today & _
"<br><br>" & _
"Instructions: <br>" & Instructions & _
"<br><br>" & _
"Please approve " & workflowProperties.Item.File.Name & _
"<br><br>" & _
"To complete this task:" & _
"<br><br>" & _
"    1. Review " & "<a href='" & workflowProperties.SiteUrl & "\" & workflowProperties.ItemUrl & "'>" & workflowProperties.Item.File.Name & "</a>" & "." & _
"<br>" & _
"    2. Use the <b>Edit this task</b> button to Approve/Reject the document." & _
"<br><br>" & _
"To view this workflow's history, click " & "<a href=" & workflowProperties.SiteUrl & "/_layouts/WrkStat.aspx?List=" & workflowProperties.ListId.ToString & "&WorkflowInstanceID=" & workflowProperties.WorkflowId.ToString & ">here</a>" & "." & _
"<br><br>" & _
"<font color=red>Comments:</font> <br>" & Comments & _
"</span>"

5. go back to the design surface and double-click PeerReviewerApprovalActivities to drill down into it

6. drag and drop a SendEmail activity between hlogPeerReviewerApproved and hlogWorkflowCompleted
and set the following properties:
a. Name: sendPeerReviewerApprovedEmail
b. Body: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
c. CC: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
d. CorrelationToken: choose workflowToken from dropdownlist
e. Subject: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
f. To: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
g. MethodInvoking: type InitiatorApprovalTaskEmail and hit enter
h. You will be taken to the code stub for PeerReviewerApprovedEmail. Copy and paste the following code:

'get Initiator's email address
Dim InitiatorObject As SPUser = GetUserObject(workflowProperties.Originator)
Dim InitiatorEmail = InitiatorObject.Email

'get CC's email address
Dim CCObject As SPUser = GetUserObject(CC)
Dim CCEmail = CCObject.Email

'get PeerReviewer's name
Dim PeerReviewerObject As SPUser = GetUserObject(PeerReviewer)
Dim PeerReviewerName = PeerReviewerObject.Name

sendPeerReviewerApprovedEmail_To = InitiatorEmail
sendPeerReviewerApprovedEmail_CC = CCEmail
sendPeerReviewerApprovedEmail_Subject = workflowProperties.Item.File.Name & " has been approved by " & PeerReviewerName & ". Peer Review Complete."
sendPeerReviewerApprovedEmail_Body = "<span style='font-family: arial; font-size: medium'>" & _
workflowProperties.Item.File.Name & " was approved by " & PeerReviewerName & " on " & DateTime.Now & _
"<br><br>" & _
"Peer Review Complete" & _
"<br><br>" & _
"<span style='font-family: arial; font-size: x-small'>" & _
"To view this workflow's history, click " & "<a href=" & workflowProperties.SiteUrl & "/_layouts/WrkStat.aspx?List=" & workflowProperties.ListId.ToString & "&WorkflowInstanceID=" & workflowProperties.WorkflowId.ToString & ">here</a>" & "." & _
"</span></span>"

7. drag and drop a SendEmail activity between hlogPeerReviewerRejected and setStateInitiatorApproval
and set the following properties:
a. Name: sendPeerReviewerRejectedEmail
b. Body: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
c. CC: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
d. CorrelationToken: choose workflowToken from dropdownlist
e. Subject: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
f. To: click the ellipses, click the "Bind to a new member" tab,
remove the 1 from the end of the "New member name" and click OK
g. MethodInvoking: type InitiatorApprovalTaskEmail and hit enter
h. You will be taken to the code stub for PeerReviewerRejectedEmail. Copy and paste the following code:

'get Initiator's email address
Dim InitiatorObject As SPUser = GetUserObject(workflowProperties.Originator)
Dim InitiatorEmail = InitiatorObject.Email

'get CC's email address
Dim CCObject As SPUser = GetUserObject(CC)
Dim CCEmail = CCObject.Email

'get PeerReviewer's name
Dim PeerReviewerObject As SPUser = GetUserObject(PeerReviewer)
Dim PeerReviewerName = PeerReviewerObject.Name

sendPeerReviewerRejectedEmail_To = InitiatorEmail
sendPeerReviewerRejectedEmail_CC = CCEmail
sendPeerReviewerRejectedEmail_Subject = workflowProperties.Item.File.Name & " has been rejected by " & PeerReviewerName
sendPeerReviewerRejectedEmail_Body = "<span style='font-family: arial; font-size: medium'>" & _
workflowProperties.Item.File.Name & " was rejected by " & PeerReviewerName & " on " & DateTime.Now & _
"<br><br>" & _
"<span style='font-family: arial; font-size: x-small'>" & _
"To view this workflow's history, click " & "<a href=" & workflowProperties.SiteUrl & "/_layouts/WrkStat.aspx?List=" & workflowProperties.ListId.ToString & "&WorkflowInstanceID=" & workflowProperties.WorkflowId.ToString & ">here</a>" & "." & _
"</span></span>"

Posts in this series:
Part 1: Introduction
Part 2: Create the initiation form
Part 3: Create the task form
Part 4: Create the state machine workflow
Part 5: Add workflow history logging
Part 6: Add task notification emails

1 comment:

  1. when you bind to a new member. do you bind to a property or to a field?

    ReplyDelete