RSS LOGORss   Welcome Guest  Login Register
Welcome to our Blogs
Back Back
Subject


mag
2008/06/08 03:06PM
Dynamic Email Fetcher      Number of views: 1593

Hi, this class is used to prepare emails, it was used in SharePoint Workflows to send emails to a supervisor or something, anyway, it uses an XML file that contains a list of emails with an ID, it loads the XML then, gets the Email with XPath query, then replaces the email body with a parameters set defined in the Email block as well along with it's body,

if you read the code you will find some tricks, for Time and Date conversion, plus, if you have a list of values (choices) in a field and you want to replace it with a readable text (look at the sample xml below).

<-- Starts here -->

public class EmailFetcher
    {
        public static string PopulateEmail(string ID, Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties Wfp)
        {
            string XMLPath = @"C:\...\Emails\WFEmails.xml";// System.Configuration.ConfigurationManager.AppSettings["EmailXMLPath"];
            string EmailBody = "";
            string CurrentParameter = "";

            try
            {
                System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();

                xdoc.Load(XMLPath);

                System.Xml.XmlNodeList node = xdoc.SelectNodes("//Email[@ID='" + ID + "']");

                if (node.Count > 0)
                {
                    System.Xml.XmlNodeList pList = node[0].SelectNodes("Parameters");
                    EmailBody = node[0].InnerText;

                    foreach (System.Xml.XmlNode param in pList[0])
                    {
                        CurrentParameter = param.Attributes["ID"].Value;
                        string tempVal = (Wfp.Item[param.Attributes["Value"].Value] != null) ? Wfp.Item[param.Attributes["Value"].Value].ToString() : "";
                        if (param.Attributes["Type"].Value != "")
                        {
                            switch (param.Attributes["Type"].Value)
                            {
                                case "Date":
                                    tempVal = DateTime.Parse(tempVal).ToString("dd/MM/yyyy");
                                    break;
                                case "Time":
                                    tempVal = DateTime.Parse(tempVal).ToString("hh:mm:ss tt");
                                    break;
                                case "ReplaceValue":
                                    string[] Replacements = param.Attributes["Keywords"].Value.Split(new char[] { ',' });
                                    string[] ReplaceBy = param.Attributes["ReplaceBy"].Value.Split(new char[] { ',' });
                                    int i = 0;
                                    foreach (string replace in Replacements)
                                    {
                                        if (replace.ToLower().Trim() == Wfp.Item[param.Attributes["Value"].Value].ToString().ToLower())
                                        {
                                            tempVal = ReplaceBy[i];
                                            break;
                                        }
                                        i++;
                                    }
                                    break;
                            }
                        }

                        EmailBody = EmailBody.Replace(param.Attributes["ID"].Value, tempVal);
                    }
                }

            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                EmailBody += "<h2>Error in Current Parameter: " + CurrentParameter + "</h2>";
            }
            return EmailBody;
        }
    }

<-- Ends here -->

 

and here is a sample for an XML file contains emails

<?xml version="1.0" encoding="utf-8"  ?>
<Emails>
  <Email ID="22">
      <Parameters>
        <Parameter ID="%FFRName%" Value="FFRName" Type=""/>
        <Parameter ID="%EditLink%" Value="ViewPageURL" Type=""/>
        <Parameter ID="%Approval%" Value="FRFRequestStatus" Type="ReplaceValue" Keywords="UserApproved,UserDeclined" ReplaceBy="No,Yes"/>
      </Parameters>
      <EmailBody disable-output-escaping="yes">
        <![CDATA[
					  <div style="direction: rtl; text-align: right; border: solid 1px black; width: 700px;
        padding-right: 10px; padding: 10px 10px 10px 10px; font-family: Arial; font-weight: bold;
        font-size: 14px;">
        <p>
            %FFRCorrespondantName%
        </p>
        <p style="padding-right: 30px;">
            
        </p>
        <div style="text-align:center;">
            <p style="text-align: center; border-bottom: solid 1px black; width: 150px;">
               Subject text sample
            </p>
        </div>
        <p>
              ( %Approval% )  
        </p>
        <p>
         (<a href='%EditLink%'> Link </a>).
        </p>
        <p style="text-align: center;">
            Best wishes
        </p>
    </div>
					 ]]>
		</EmailBody>
	</Email>
</Emails>
Best wishes,
 
MAG