In most scenarios, we use SharePoint list to store contents like subject, mail body, and so on. However, when the mail count gets very high, you might end up seeing some performance issues. To overcome these performance issues, we can store the mail template as XSLT, and take advantage of XslCompiledTransform class to generate mail contents during runtime.

 

The XslCompiledTransform class (Transforms XML data using an XSLT style sheet) has been enhanced in .NET Framework 4.0 version. Let’s take a look at two important overloaded methods – Load (IXPathNavigable, XsltSettings, XmlResolver) and Transform (IXPathNavigable, XsltArgumentList, TextWriter)

• Load method compiles the XSLT style sheet contained in the IXPathNavigable.
• Transform method executes the transform using the input document specified by the IXPathNavigable object, and outputs the results to a TextWriter. XsltArgumentList provides additional run-time arguments.

 

Below, you can find code snippet which can be used to generate email contents.

 

public static bool SendEmailUsingXSLTTemplate(SPWeb webObj, string emaiAddresslTo, string xsltTemplateFileURL, IDictionary xslValues)

 

{
XmlDocument xmlEmailDocument; XPathNavigator xpathNavigator;
XslCompiledTransform xslEmailTransform = new XslCompiledTransform();
XsltArgumentList xslArgumentList;StringBuilder sbEmail;
XmlTextWriter xmlEmailWriter;XmlNode xmlTitleNode;XmlDocument xmlEmail;
}

 

In the above method the parameters used are:

• xsltTemplateFileURL holds the URI of the XSLT file.
• 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 an extension object to xslargument. The advantage of adding xslargument is that we can access the class properties directly from XSLT. For example get_ProjectName() is a property from the request class.

 

Below, you can find code snippet that will show how to load and transform the XSLT template to create the email contents.

 

Code-Snippet-1

Code-Snippet-2

 

XsltSettings xsltsettings = new XsltSettings(true, true);

XmlUrlResolver urlResolver = new XmlUrlResolver();

urlResolver.Credentials = CredentialCache.DefaultCredentials;

string strEmailSubject  = 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.

xslTransform .Load(xsltTemplateFileURL, xsltsettings, urlResolver);

xmlEmailDocument = new XmlDocument();

xmlEmailDocument.AppendChild(xmlEmailDocument.CreateElement(“DocumentRoot”));

xpathNavigator = xmlEmailDocument.CreateNavigator();

xslArgumentList = new XsltArgumentList();

if (xslValues != null)

{

foreach (DictionaryEntry xslEntry in xslValues){

xslArgumentList.AddExtensionObject(xslEntry.Key.ToString(), xslEntry.Value);

}

}

sbEmail = new StringBuilder();

xmlEmailWriter = 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.

xslTransform .Transform(xpathNavigator, xslArgumentList, xmlEmailWriter);

xmlEmail = new XmlDocument();

xmlEmail.LoadXml(sbEmail.ToString());

xmlTitleNode = xmlEmail.SelectSingleNode(“//title”);

strEmailSubject  = xmlTitleNode.InnerText;

//send email using smtp configuration

return SendEmailUsingXSLTTemplate(webObj, emaiAddresslTo, strEmailSubject , sbEmail.ToString());

}

catch (Exception ex)

{

Utilities.LogError(ex); return false;

}

 

Biju
Biju
author