In most of the scenarios we use sharepoint list to store the contents like subject, mail body etc.
However when mail count goes high this approach might result in performance issues.
To overcome performance issues we can store the mail template as XSLT and take advantage of XslCompiledTransform class to generate mail contents run time.
XslCompiledTransform class ( Transforms XML data using an XSLT style sheet ) has been enhanced a lot in .NET Framework 4.0 version.
Two important overload methods to look are Load(IXPathNavigable, XsltSettings, XmlResolver) and Transform(IXPathNavigable, XsltArgumentList, TextWriter)
Find the below code snippet which is used to generate email contents in one of the project I have developed.
public static bool SendEmail(SPWeb web, string emailTo, string xsltTemplateFile, IDictionary xslValues)
{
XmlDocument xmlDoc;XPathNavigator xpathNavigator;
XslCompiledTransform xslEmailTransform = new XslCompiledTransform();
XsltArgumentList xslArguments;StringBuilder sbEmail;
XmlTextWriter xmlWriter;XmlNode xmlNodeTitle;XmlDocument xmlEmail;
}
In the above method the parameters are :
xsltTemplateFile holds the URI of the XSLT file.
And IDictionary holds the class instance whose property values will be used to generate the email contents. xsltValues = new Hashtable(1);
xsltValues.Add("fba:Request", request);
Request request = new Request();
request.ProjectURL = url;
request.ProjectName = projectName;
etc..This object is added as extension object to xslargument. Advantage of adding xslargument is we can acess the class properties directly from XSLT, for example get_ProjectName() is a property from the request class.
XsltSettings settings = new XsltSettings(true, true);
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials
string subject = string.Empty;
try
{
//Loads and compiles the XSLT style sheet specified by the URI
//The XmlResolver resolves any XSLT import or include elements and the
//XSLT settings determine the permissions for the style sheet.
xslEmailTransform.Load(xsltTemplateFile, settings, resolver);
xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateElement("DocumentRoot"));
xpathNavigator = xmlDoc.CreateNavigator();
xslArguments = new XsltArgumentList();
if (xslValues != null)
{
foreach (DictionaryEntry xslEntry in xslValues)
{
xslArguments.AddExtensionObject(xslEntry.Key.ToString(), xslEntry.Value);
}
}
sbEmail = new StringBuilder();
xmlWriter = new XmlTextWriter(new StringWriter(sbEmail));
//Executes the transform using the input document specified by the
//IXPathNavigable object and outputs the results to an TextWriter.
//The XsltArgumentList provides additional run-time arguments.
xslEmailTransform.Transform(xpathNavigator, xslArguments, xmlWriter);
xmlEmail = new XmlDocument();
xmlEmail.LoadXml(sbEmail.ToString());
xmlNodeTitle = xmlEmail.SelectSingleNode("//title");
subject = xmlNodeTitle.InnerText;
//send email using smtp configuration
return SendEmail(web, emailTo, subject, sbEmail.ToString());
}
catch (Exception ex)
{
Utils.LogError(ex);
return false;
}
No comments:
Post a Comment