Tuesday, 29 April 2014

How to send alert (Reminder Email) in SharePoint Workflow - Visual Studio.

Blog approach - In many projects  it's a requirement to send an email on daily basis or hourly.
Using Delay Activity  we can send an alert.

To achieve above requirement we need to use listen Activity , delay Activity and code activity.

Listen Activity : The listen activity will wait for any of the branch to wake up and whichever wakes up first - completes the activity. The Listen Activity activity cannot be used in state machine workflows.

Delay Activity : Here we have to set time span / Initialize time out duration .

Code Activity : Once in time of delay activity is up , code activity will be execute where we will write send mail code.


Put listen activity with two branches under while loop .

One branch will be use for execute the code on  "OnTaskChanges"  and another branch will be use for execute thye code on Dealy Activity ( which will use for send an email ) .

Here is a code activity (codeActivitySendReminder)  which will execute the code for send an alert if the time in dealy activity is up.

Here is a delay Activity handler code :

private void delayActivity_InitializeTimeoutDuration(object sender, EventArgs e)
{
   DelayActivity dlyAct = sender as DelayActivity;
   dlyAct.TimeoutDuration = new TimeSpan(<Number Of Days Here>, 0, 0, 0);
}

Now let's say we want to send a reminder every 2 days  if the task has not completed,  then the above line would look like

dlyAct.TimeoutDuration = new TimeSpan(2, 0, 0, 0);


Some explanation:

If you set <Number Of Days> as 1 (i.e. 1 Day),So It will consider as 24 Hr means time out duration will up after 24 Hr and then fire the code within Code Activity i.e. CodeActivitySendReminder.

Consider an example- If Time out duration of delay activity is initialized at 3:00 PM on dated 1/1/2014. So Time of delay activity will up at 3:00 PM of 2/1/2014 and hence code activity (CodeActivitySendReminder)  fires up.

If You want to send Reminder mail at particular time , suppose at 3:00 AM.

Calculate the value for 3:00 AM as per 24 Hr. Time and it will be 27.

private void delayActivity_InitializeTimeoutDuration(object sender, EventArgs e)        {
   int TaskGenerateTime = DateTime.Now.TimeOfDay.Hours;
   int AlertFiredTime = 27 - TaskGenerateTime;
   DelayActivity dAct = sender as DelayActivity;
   dAct.TimeoutDuration = new TimeSpan(AlertFiredTime, 0, 0);
}

private void CodeActivitySendReminder(object sender, EventArgs e)
{
    //TODO: code for send mail.

}

Going further more: 

The above code can be replaced with the days based on actual Due date of the task in CreateTask activity.
 Example:  
TimeSpan ts = TaskObject.DueDate -  DateTime.Today ; // (Calculate ts before the While loop above)
and hence use 

ts.Days above i.e.
dlyAct.TimeoutDuration = new TimeSpan(ts.Days, 0, 0, 0); 

1 comment:

  1. Hello Anup,

    Good post!!. I have one requirement for send reminder email before 3 days if task is not completed. Can you please guid me or provide me source code as you have developed

    Thanks,
    Ravi

    ReplyDelete