Thursday, August 7, 2008

A code comparison: SendEmail activity vs. SPUtil.SendEmail

Although shorter is not necessarily faster or easier to understand (much less is shorter always better), the code length of a given way of tackling a problem is nonetheless a useful metric for how valuable it is. So today I shall compare code length of two means of sending email in a SP workflow.

1. Use a SendEmail activity.

We can actually do this a few different ways:

  1. Bind the activity's properties to variables in your workflow and set the values pseudo-statically:
    private mySendEmailActivity_From = "From Address";
    private mySendEmailActivity_To = "To Address";
    private mySendEmailActivity_Subject = "Subject";
    private mySendEmailActivity_Body = "Body";

  2. (Some say this causes runtime errors!) Just set the properties on the activity itself prior to running it:
    private void notifyHr_MethodInvoking(object sender, EventArgs e)
    mySendEmailActivity.To = "To Address";
    mySendEmailActivity.From = "From Address";
    mySendEmailActivity.Subject = "Subject";
    mySendEmailActivity.Body = "Body";
  3. Bind the properties statically AND set them dynamically, which would be about the length of the above two examples added together.

2. Code it yourself!

Many find it more reliable to insert a Code activity and bind it to the following procedure:
public void sendRejectionEmail()
SPUtility.SendEmail(workflowProperties.Web, true, false, workflowProperties.OriginatorEmail, "HR-1 Rejected!", "pwn'd by the approvers! Mwahahaaaa!!!");
That's just one long line of code.

So... using the above one-liner should not take any longer to write than using the SendEmail activity.

No comments: