Wednesday, May 25, 2016

SharePoint Timer Job: Sending notifications with dynamic email templates stored in SharePoint list

I was recently working on a project requirement that involved setting up the alert system on a SharePoint list and system had to send notifications based on the due date. The email content is dynamic in nature and it is based on due date of the item. For instance  we need to send an particular kind of notification if the filing is due in 7 days and other set of notification if the filing is due in 15 days.

My intention was to create self service email template that would reside in SharePoint list as shown below such that user can change the wording if needed without developer intervention or deployment of code.

The fields with in the square brackets are read by SharePoint custom code and it will be auto populated by system based on list item when the timer job runs.




I had created hash table with all the required fields and will parsing the above template.
[code]String emailBody = string.Empty;
String emailsubject = string.Empty;
SPList spList = web.Lists.TryGetList("MessageTemplates");
SPQuery messageTemplateQuery = new SPQuery();
messageTemplateQuery.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>StandardRemainder</Value></Eq></Where>";
messageTemplateQuery.RowLimit = 1;
messageTemplateQuery.ViewFields = "";
SPListItemCollection listitem = spList.GetItems(messageTemplateQuery);
SPListItem item0 = listitem[0];
emailBody = item0["BodyHTML"].ToString();
emailsubject = item0["Subject"].ToString();
Hashtable hashTableForMessageBody = null;
hashTableForMessageBody = CreateHashTableForMessageBody(title, description, regulatoryFilingEntity, regulatoryFiling, FilingDueDate, Duein);
emailBody = GetEmailBody(web, hashTableForMessageBody, emailBody);
[/code]
When the above "CreateHashTableForMessageBody" is called following method will be executed. It basically stores all the information in hashtable.

[code]private Hashtable CreateHashTableForMessageBody(string title, string description, string regulatoryFilingEntity, string regulatoryFiling, DateTime FilingDueDate, int Duein)
        {
            Hashtable hashTableForMessageBody = new Hashtable();
            try
            {
                hashTableForMessageBody.Add("Title", title);
                hashTableForMessageBody.Add("Description", description);
                hashTableForMessageBody.Add("Regulatory Entity", regulatoryFilingEntity);
                hashTableForMessageBody.Add("Regulatory Filing", regulatoryFiling);
                hashTableForMessageBody.Add("Filing Due Date", FilingDueDate);
                hashTableForMessageBody.Add("Due In", Duein);              
            }

            catch (Exception ex)
            {
                EventLog.WriteEntry("Regulatory Calendar Error", "CreateHashTableForMessageBody\r\n" + ex.Message, EventLogEntryType.Error);
            }
            return hashTableForMessageBody;
        }


        //Parses the message template from list and sets the relevant subject
        private string GetEmailBody(SPWeb web, Hashtable hashTableForMessageBody, string body)
        {
            try
            {
                foreach (object obj in hashTableForMessageBody.Keys)
                {
                    body = body.Replace("[" + obj.ToString() + "]", hashTableForMessageBody[obj].ToString());
                }
            }

            catch (Exception ex)
            {
                EventLog.WriteEntry("Regulatory Calendar Error", "GetEmailBody\r\n" + ex.Message, EventLogEntryType.Error);
            }
            return body;
        }
[/code]

No comments:

Post a Comment