Navigation Bar

Tuesday 24 April 2012

Email template manager for SharePoint and C# developer


Email Templates Utility Description
This framework or utility will help you to manage email templates and allows you to integrate with your project easily.

There are scenarios when you need to send emails with customised text or template based in SharePoint or Asp.net or any C# project. This tool will help you achieve this goal.

Source code:
http://emailtemplatemanager.codeplex.com/

How to use it
This is very easy to include to your project. Following are the steps:
1.       Clone this project/source code and include in your target project.
2.       Implement AbstractTemplateManager class according to your need. For test purpose I have created TestTemplateManager in the source code. I have also created a SharePointTemplateManager just to give you an idea how to do it for SharePoint.
a.       When you implement AbstractTemplateManager you need to implement following methods:
                                                               i.      GetTemplate(string templateId)
You need to implement this method to let the framework know about the template that will be going to use for applying values

                                                             ii.      BeforeApplyingTemplateOnSubject
There would be some situation where you need to apply some custom logic before going to the framework.
If you don’t want to perform any custom logic then leave the method implementation blank but DO NOT throw any exception.

                                                            iii.      AfterApplyingTemplateOnSubject
There would be some situation where you need to perform some custom logic after applying values from framework.

For sample purpose I have added an implementation in TestTemplateManager class.

                                                           iv.      BeforeApplyingTemplateOnBody
                                                             v.      AfterApplyingTemplateOnBody
3.       Implement IItem interface. This interface is nothing but contains field names and their values. If you are implementing for SharePoint, then field names become SharePoint column/field name and value you can get SPListItem[fieldname]
4.       There is an enum which defined what type of Manager we have got in the system. Currently I have added
public enum EnumTemplateType
{
         SharePoint,
         Xml,
        Test
 }

This type lets the framework know where all templates are managed.
SharePoint: It means all templates are managed in SharePoint list.
Xml: It means all templates are managed in Xml file.
Test: It is just for testing purpose.

5.       In the consumer project do the following:

Get instance of template manager using Factory method as shown in below.
Here is the code to run the test project:
static void Main(string[] args)
        {
            IItem orderItem = new Item();
            orderItem.FieldAndValues.Add("DELIVERY_DATE", new DateTime(2012, 1, 1).ToString());
            orderItem.FieldAndValues.Add("CUSTOMER", "Unique world");

            AbstractTemplateManager manager = TemplateManagerFactory.Create(EnumTemplateType.Test);
            Email email = manager.ApplyTemplate("order_template", orderItem);

            //just for testing purpose
            Console.WriteLine("Displaying template...");
            EmailTemplate template = manager.GetTemplate("order_template");
            Console.WriteLine("Template.Subject: {0}", template.Subject);
            Console.WriteLine("Template.Body: {0}", template.Body);
            Console.WriteLine("");

            Console.WriteLine("After applying template...");
            Console.WriteLine("Email.Subject: {0}", email.Subject);
            Console.WriteLine("Email.Body: {0}", email.Body);

            Console.ReadKey();
        }