Search This Blog

Wednesday, October 30, 2013

Send customized email Alerts from SharePoint Workflow (2007 / 2010 / 2013)

The purpose of this blog post is to discuss a design which can send customized email alerts / reminders from a SharePoint workflow.

Skills Needed: 
C# and SharePoint custom workflows

Let's have a look at the different methods:

a) Use SPAlert
This works okay however the problem using SPAlert object is the customization of the message.
There are ways to modify/create custom messages by changing the XML files in 14 hive but i prefer not to change those.

b) Using Listen activity

We need a customized email message to be sent to the workflow users i.e. be it approvers or delegates etc. and here is a design which can provide that functionality in a custom SharePoint workflow.

The CreateTask activity (not shown in the image above) is above the While Activity i.e. while loop.

Legends for the Image:
1:   Listen Activity
2.  Delay Activity
3.  Code Activity

Nested inside the While loop is a Listen Activity (1)  with two branches as shown above. Both the branches must start with a Event Driven activity. Example: OnTaskChanged, DelayActivity etc.

The listen activity will wait for any of the branch to wake up and whichever wakes up first - completes the activity.

Hence in above case either the user completes the task (ontaskchanged1 fires) OR the time in delay activity is up (delayActivity1 completes) and hence the code activity fires up. The code activity above i.e. codeActivitySendReminder contains custom email code i.e. Email reminders for Task

Here is what the delay Activity handler looks like:

  private void delayActivity1_InitializeTimeoutDuration(object sender, EventArgs e)
            DelayActivity dAct = sender as DelayActivity;
            dAct.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:
     dAct.TimeoutDuration = new TimeSpan(2, 0, 0, 0);       

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

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

How to send custom emails is out of scope for this article since that is very common piece of code around and easily available on internet.

And that's all for this post.

Any questions / comments are welcome.

Please write back...

-- Mohan

( By the way I saw this on linkedin:
Question: Why do Java developers need eye glasses?
Answer:   Because they do not C# 

No comments: