Handy Email Utility for any SharePoint Project

I have worked on several SharePoint projects so far in my career. One of the most common requirement in those projects was the ability to send custom email alerts and notifications.

The projects need to send email through code. The code can be a custom page, web part code, email alerts on a list, custom visual studio workflow, etc. The simple solution could be to send the hard coded emails in a code. But often this won’t be the solution that will accepted by most of the clients and customers. They often want to control the email content getting sent across to people in organization or people outside. Also client likes to have ability to configure the emails are which can be static as well as dynamic content at some level.

One of the best and easiest solution to provide configurable emails is to use SharePoint’s most common feature i.e a custom list. The list will store the different email templates identified by some unique email template id. Another part of this solution will be some APIs to get these email templates in any sort of code and then send the emails accordingly.

But over the period, projects get changed and the same efforts are almost repeated every time. So in order to save this time I decided to create a utility project which will provide generic solution for this requirement and much more features. Fortunately the codeplex.com provides a great platform to create and manage the projects which can be helpful to the SharePoint community across the world.

The project is located at codeplex at https://spemailutility.codeplex.com.

SPEmailUtility on CodePlex.com

SPEmailUtility on CodePlex.com

The source code and solution packages are uploaded on codeplex site here.

Features:

1. Ability to configure the dynamic emails

This utility project provides the ability configure the dynamic subject and body of the email templates. This is achieved by using the token for example <%WebUrl%> and then replace the tokens by actual values in code before sending out the custom email alerts.

Any special characters can be used to define the tokens. But the advantage of using <%%> characters is that if somehow some values are not replaced, they are not visible in the email.

New Email Template Form:

New email template form looks like below. As seen email template has four fields.

1. Email Template Id

This is the unique string id to identify the email template uniquely. This field is intentionally kept as drop down to avoid any typo errors while creating different email templates. The possible values for this are separately manged in list settings by the administrator.

2. Short Description

This is a brief description about the purpose of given email template.

3. Email Template Subject

This can be the static or dynamic subject of the email template. The tokens such as <%TaskTitle%> can be used in subject configuration and replaced by actual Task Title in code before sending the email.

4. Email Template Body

This can also be the static or dynamic content of the email body. The tokens need to be replaced by the code.

New Email Template Form

Email Template Configuration List:

The SharePoint feature in this SPEmailUtility project adds email template configuration list to store the email templates. SharePoint site administrators or users who manage SharePoint portal can be given permissions to manage this list in order configure the different email templates required to use in SharePoint projects.

Email Template Configuration List

Email Template Configuration List

Email Configuration Manager APIs:

In this project I have provided three important APIs to use the email configuration templates.

1. EmailTemplate GetEmailTemplate(string emailTemplateId);

If you want to get an email template which has static subject and body, use this API.

2. EmailTemplate GetEmailTemplate(string emailTemplateId,
IDictionary<string, string> subjectProperties,
IDictionary<string, string> bodyProperties);

If you want to get the dynamic email template with subject and bodies which contains dynamic tokens. The user of the code has to provide name and value pairs for these tokens to be replaced.

3. void AddEmailTemplate(string emailTemplateId, string shortDescription,
string emailTemplateSubject, string emailTemplateBody);

This is optional API to use to add email templates through code.


interface IEmailConfigurationManager
{
/// <summary>
/// Gets Email Template by it's ID
/// </summary>
/// <param name="emailTemplateId"></param>
/// <returns></returns>
EmailTemplate GetEmailTemplate(string emailTemplateId);

/// <summary>
/// Gets Email Template by it's ID by updating the email template properties
/// </summary>
/// <param name="emailTemplateId"></param>
/// <param name="subjectProperties"></param>
/// <param name="bodyProperties"></param>
/// <returns></returns>
EmailTemplate GetEmailTemplate(string emailTemplateId,
                        IDictionary<string, string> subjectProperties,
                        IDictionary<string, string> bodyProperties);

/// <summary>
/// Add new email template.
/// </summary>
/// <param name="emailTemplateId"></param>
/// <param name="emailTemplateSubject"></param>
/// <param name="emailTemplateBody"></param>
void AddEmailTemplate(string emailTemplateId, string shortDescription,
                      string emailTemplateSubject, string emailTemplateBody);
}

Still working on the post…

Cheers,
Rahul Babar

Advertisements

4 thoughts on “Handy Email Utility for any SharePoint Project

    • Yes, these are the list items only. The idea here is to keep templates external to the source code. So if there is any change in the template, source code changes are not required and hence redeploying the solution is not required. The templates can be changed by end user/admin with appropriate permissions on the list.

      Thanks,
      Rahul Babar

    • You can use below API method with the dictionary of tokens (name-value pairs ex “FirstName”: “Rahul”, “LastName”:”Babar”). The API should be written to find and replace each token from Subject string and Body string.

      EmailTemplate GetEmailTemplate(string emailTemplateId, IDictionary subjectProperties, IDictionary bodyProperties);

      You can refer sample source code for more details https://spemailutility.codeplex.com/SourceControl/latest

      Please let me know if that helps you.

      Thanks,
      Rahul Babar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s