Home > SharePoint, Vs2010 > SendMail activity problem in Visual Studio 2010 workflows

SendMail activity problem in Visual Studio 2010 workflows

2012/02/05

I’m evaluating a book on SharePoint 2010 workflows (next post …) because i was interested especially to workflows developed with Visual Studio 2010.

In a While activity i have inserted a Sequence activity (it is possible to insert only an activity in a While) and inside this Sequence i have inserted my logic (an If Else).

In some Else branch i was inserting an SendMail, and surprise: the mail sending was not working even if in debug (when i was editing the List item and the workflow goes on) i have seen that the code for send the email was hit , but the email was not really sent as confirmed by smtp4dev.

The email system (based on hMailServer) was working , for example for an alert relative to a List item.

Casually i have discovered that re-editing the same item …the email was working ! but i can’ ask to customers to re-edit the List item…and in every case the logic was based on the state change.

This is the schema:


The WorkFlowItemChanged has this setting for BeforeProperties (the same thing for After):


The first branch (ifStatusHasNotChanged) has in the book this logic:

// works only for document library
if (onWorkflowItemChanged1_BeforeProperties1["Status"] == null)
{
    // incidental : it is Pending assignment so it anyway works
    e.Result = false;
    return;
}
string bStatus = onWorkflowItemChanged1_BeforeProperties1["Status"].ToString();
if (bStatus == aStatus)
    e.Result = true;
else
    e.Result = false;

As stated in my comments, i have seen that the BeforeProperties are not available for a List (bug? by design? my mistake in something?) but are available instead in a Document Library; so the logic is incidentally correct .

In this case should be similar to the logic of other branches:

string aStatus = onWorkflowItemChanged1_AfterProperties1["Status"].ToString();
if (aStatus == "Pending Assignment")
    e.Result = true;
else
    e.Result = false;

In the second branch (Pending Completion, but the problem was similar for other branches) the code for sending the email was:

private void sendEmailToAssignee_MethodInvoking(object sender, EventArgs e)
{
    SPListItem wfItem = onWorkflowActivated1.WorkflowProperties.Item;
    SPFieldUser assignedTo = (SPFieldUser)wfItem.Fields["Assigned To"]; 
    SPFieldUserValue user = (SPFieldUserValue)assignedTo.GetFieldValue(wfItem["Assigned To"].ToString()); 
    string assigneeEmail = user.User.Email;
    sendEmailToAssignee.To = assigneeEmail; 
    sendEmailToAssignee.Subject = "New work order has been created.";
    sendEmailToAssignee.Body = "Work order number " + onWorkflowActivated1.WorkflowProperties.Item.ID + " has just been created and assigned to you.";
}

But as already written, when the code passes for the first time when editing a List item, no email sent.

Finally i have tried to work with a binded field , for example in the SendEmail activity properties i have done this double by clicking the little yellow icon:


I have binded a Field for Body To Subject in all of my SendEmails, it was generated this code:

public String sendEmailToAssignee_Body1 = default(System.String);
public String sendEmailToAssignee_Subject1 = default(System.String);
public String sendEmailToAssignee_To1 = default(System.String);
public String sendEmailToRequesterHold_Body1 = default(System.String);
public String sendEmailToRequesterHold_Subject1 = default(System.String);
public String sendEmailToRequesterHold_To1 = default(System.String);
public String sendEmailToRequestorComp_Body1 = default(System.String);
public String sendEmailToRequestorComp_Subject1 = default(System.String);
public String sendEmailToRequestorComp_To1 = default(System.String);

My SendEmailToAssignee activity is changed to (similar change for every SendEmail) and the emails finally are immediately sent:

private void sendEmailToAssignee_MethodInvoking(object sender, EventArgs e)
{
    SPListItem wfItem = onWorkflowActivated1.WorkflowProperties.Item;
    SPFieldUser assignedTo = (SPFieldUser)wfItem.Fields["Assigned To"]; 
    SPFieldUserValue user = (SPFieldUserValue)assignedTo.GetFieldValue(wfItem["Assigned To"].ToString()); 
    string assigneeEmail = user.User.Email;
    sendEmailToAssignee_To1 = assigneeEmail; 
    sendEmailToAssignee_Subject1 = "New work order has been created.";
    sendEmailToAssignee_Body1 = "Work order number " + onWorkflowActivated1.WorkflowProperties.Item.ID + " has just been created and assigned to you.";
}

I don’t understand because the direct assignment to properties does not work correctly: could be that the book author was working on a SharePoint 2010 version without the latest patches, that could have been caused the problem; and the BeforeProperties that apparently are not available in a List is another big issue.

Perhaps the problem is related to the While activity that is simulating an State machine in a Sequential workflow, i should try in others workflows with a different logic.

Another problem that i have encountered is that the same schema applied to workflow linked to a Document Library works as for the List, but when the workflow is terminated in the List is all ok, in the Document Library the workflow is restarted on the same item!


In this case i have terminated the workflow by hand , i will investigate more about this problem.

Categories: SharePoint, Vs2010