Blog Archives

October 2010, an update!

So a year on from starting at IE, what’s news? Besides spring and the joy of hayfever again, I thought I’d give a run down on what’s been keeping me busy.

Hoyts.com.au and hoyts.co.nz; the website for the cinema chain that operates here and in NZ is both a business and technical challenge on many levels. Dealing with external vendors, multiple stakeholders and a codebase that was far from Sitecore best practice has been a learning experience. In the year since we’ve managed to put live a home page redesign, regular maintenance updates and bug fixes, and most importantly; the new rewards program which is going strong.

Hoyts Corporate Australia and New Zealand; architecturally what should have been a fairly straight-forward exercise to create a ‘microsite’ running off a main site presented it’s own challenges when working around some publishing booby traps left by the previous developers. Also learnt some lessons about working with a multi-site Sitecore installation.

Hoyts Mobile Australia and New Zealand; a couple of brand new Hoyts websites, no Sitecore integration. Designed to replace an older WAP mobile site, the new mobile website incorporates some of the latest HTML5 and CSS3 techniques to streamline front-end development. Server side, it was awesome to work with MVC2, not having done any MVC development for a while, it took a while to get used to again.

Other stuff about mobile development and this site:

Maxxia NZ; just to make sure Sitecore isn’t the only CMS I work on, I did Level 1 and 2 Umbraco certification. Just in time to build the Maxxia NZ website. Nothing notable about the site itself, from the point of view of CMS’s though, it’s refreshing to work with a different CMS and one that really is bare-bones compared to Sitecore. Highly recommended for small/mid-level organisations looking for a .Net CMS, or for anyone not looking to pay for Sitecore’s full feature set.

IE Website; much to the amusement of the other .Net developers, I also put my hand up to play with WordPress. I’m not sure I’d call myself a WordPress guru, but I did get to play with a couple of cool new features of WordPress; custom post types and taxonomies.

Along the way, I’ve also learnt a hell of a lot more about front-end development than I expected. I’d even say I know my way around Photoshop and creating non-table layouts in CSS… it’s only taken me a few years to catch up to everyone else on this.

Finally, I couldn’t finish this post without giving a shout out to all the peeps at IE. They’re pretty sweet ;)

Special kudos to the .Net team there, the dorks… and Mig’s mum. She’s super sweet. ;)

Using Sitecore.NVelocity library to create email templates

If you find yourself sending emails from a Sitecore application and you don’t want to generate the emails in code-behind, an easy way to create basic email templates is by using the NVelocity library built into Sitecore API.

What is NVelocity?

NVelocity is a .Net port of the popular template engine Velocity written in Java. The .Net port is mostly recently maintained by the guys at Castle Project.

Sitecore’s NVelocity library

This is probably a bit older but still works fine for basic email templates.

One problem I repeatedly ran into, with both the Sitecore NVelocity library and the Castle Project NVelocity implementation was loading template files from the file system. After wasting a couple of hours on this (repeatedly the template file could not be found, no matter what I tried) I gave up and loaded the templates into memory using a TextReader and then applied the template using VelocityHelper.Evaluate.

Unfortunately there is nowhere near enough documentation online about NVelocity issues with reading template files, or the Sitecore NVelocity library to get much help with this problem. Basically any call to load a template file (using GetTemplate) would result in a ResourceNotFoundException.

Example of a NVelocity email template

Thank you for your registration

Your username was: $username

The delivery details you gave were:

Address: $address
City: $city
State: $state

Thank you

The file above should be saved somewhere as a .vm file, for example template1.vm. This is a basic email template for NVelocity, you use the dollar sign followed by a variable name to define somewhere that content is replaced, so in the example ‘username’, ‘address’, ‘city’ and ‘state’ are template data values.

Replacing content

This is the implementation that worked for me.

private void SendEmail()
{
 System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
 System.Text.StringBuilder sb = new System.Text.StringBuilder();
 string filePath = HttpContext.Current.Server.MapPath("/common/templates/template1.vm");
 string template;

 if (!System.IO.File.Exists(filePath))
 throw new ApplicationException("Could not find email template file: " + filePath);

 System.IO.TextReader reader = new System.IO.StreamReader(filePath);
 template = reader.ReadToEnd();

 NVelocity.VelocityContext context = new NVelocity.VelocityContext();

 context.Put("username", "hansolo");
 context.Put("address", "1337 Isme Street");
 context.Put("city", "Built On Rock N' Roll");
 context.Put("state", "VIC");

 message.From = new MailAddress("web@testsender.com");
 message.To.Add("recipient@test.com");
 message.Subject = "Test of NVelocity as email templating engine";
 message.IsBodyHtml = false;

 NVelocity.App.Velocity.Init();
 message.Body = Sitecore.Text.NVelocity.VelocityHelper.Evaluate(context, template, "example-template-replace");

 MainUtil.SendMail(message);            
}

What this does is takes the contents of the template1.vm file and replace all the values found inside using the values defined in the context.Put statements. Note these values don’t need to include the dollar sign.

Calling VelocityHelper.Evaluate and passing in a NVelocity VelocityContext, in-memory template (read from file system using System.IO) and log name will return the contents of the template with specified values replaced from the context passed in.

Output Email body

From the example above this is the body of the email sent.

Thank you for your registration

Your username was: wonaldng

The delivery details you gave were:

Address: 1337 Isme Street
City: Built On Rock N' Roll
State: VIC

Thank you

Useful links

Castle Project: Using NVelocity – the most referenced tutorial for this library.

Velocity @ Apache.org – Java version reference documentation.

Velocity is, on it’s own, a very strong template engine. In an advanced situation it can do a lot more than just replacing variables in a template, including things like program-control and conditional statements.

Hope this saves you some time if you ever have to go down this path.

Scripting data in SQL 2008 Enterprise Manager

Did you know this incredibly useful feature now comes with SQL 2008′s Enterprise Manager? I haven’t tested it with multiple tables that have complex relationships, but if you want to script data with more complexity, you should consider Red Gate’s SQL Data Compare.

Script Data in MS SQL Server 2008 Database Tables using Generate SQL Server Script Wizard

If neither is feasible, one of the most popular, creative and often-overlooked options is to use Microsoft Excel and write a formula to generate INSERT statements. Drag the cell corner to copy this formula for multiple rows. There are also many other code generation possibilities from this technique if you think creatively.

SQL Queries from Excel

Scripting SQL data is easy to do, make it part of your build process! Stick it in SVN and you bring version control to your database development.

Bored of Lorem Ipsum?

Check out BlindTextGenerator, a site that will generate dummy text for you with a number of exciting options.

I’m sure designers will get excited about this… boring people that they are. :p

Switch to our mobile site