Blog Archives

Shiny new blog things

Hope you all like the blog update: shiny new theme, widgets, share links and some analytics. If nothing else, this blog has been an excellent exercise in finding out just how much awesome you can build using WordPress and without writing a single line of code.

Perhaps next week I can run a competition to see who can guess exactly how many plugins I’m using.

Quite often I’m asked, “what exactly is it you do at work?”, I take this as an indication that what I do is so complex that few mortals can comprehend the answer. To try and make it easier, I found a picture that best describes my daily function as a Sitecore Paladin.

Smiting thy CMS bugs

In Sitecore, certification is only the first step. The picture above shows what I do at work on a daily basis, right down to the horse and the hordes of evil, it’s also allows me to end this blog post with; I’m on a horse.

LinkedInDiggRedditTumblrGoogle GmailPrintFriendlyEmailShare

Obscure Sitecore Features, inspired by Julian’s Flight Simulator

To the untrained eye, this blog might look a little neglected when really it’s just been taking a hiatus from publishing blog posts. More regular updates are around the corner including a project I’ve wanted to finish for almost six months.

In the meantime, I wanted to highlight an excellent blog post by Julian over at Igloo.

I’ve worked with Julian twice before and now for an account at Reactive we’re working together to create an SVN masterpiece that will allow two separate development teams to compete directly to create code conflicts. You can follow Julian on Twitter.


Microsoft Flight Simulator 9/11

This is Julian's screen most days


Around June, Julian took a break from playing Microsoft Flight Simulator and wrote a blog post about some useful but obscure features of Sitecore. Only coming across it last night, I notice that it’s only be re-tweeted five times which is a shame because it’s really useful.

The Gloo: Top 5 Obscure Sitecore Features

Hopefully this blog post and my excessive use of WordPress SEO plugins will help generate some more views for this piece. A couple of the most interesting for me were: BulkUpdateContext; is a must-have for anyone who is doing content migration using Sitecore and the third (UrlString Class) is something I’ve used before, but like many parts of the Sitecore API, aren’t widely known.

Not sure if I can come up with five of my own but here’s a couple, perhaps not-as-obscure features.

  1. Developer Center: XPath Builder: hardly obscure you might say but the number of certified developers I’ve come across who didn’t know it existed is surprising. In the CMS, open the menu and run Developer Center, when it loads click  Tools > XPath Builder from the menu.

    XPath Builder

    This tool allows a developer to test Sitecore XPath queries in the CMS against the database, these queries can then be used by the Sitecore.Data.Database methods: SelectItems, SelectItemsUsingXPath, SelectSingleItem and SelectSingleItemUsingXPath. The Sitecore Developer Network has an excellent reference on using Sitecore Query, you’ll need an SDN login to view it.

    Besides from testing query syntax, it’s also a useful way to compare query:/ vs fastquery:/ speeds.

    Here’s a basic rundown of the fields in XPath Builder:

    • Context Node: the node from which the query will be executed, this can help you test ancestor and descedant queries
    • Database selector: choose to run the query against core, master or web
    • XPath Expression: enter a Sitecore Query using query or XPath notation
    • Result: returns the results of the query (or any error messsages). The number of items returned is limited by web.config setting Query.MaxItems
  2. Utility classes in the root Sitecore namespace: they can be easy to miss if you’ve never looked in the Sitecore namespace or the documented API before, here’s a rundown of a few that I’ve found useful in the past:
    • Sitecore.Constants: system-wide constants, far too many to describe here but I’ve no doubt many places/developers have coded their own ways to get these values
    • Sitecore.DateUtil: Sitecore stores all datetimes in ISO format “yyyyMMddTHHmmss”, this class contains methods to convert this string to and from .Net’s DateTime type
    • Sitecore.DaysOfWeek enum: pretty straight-forward
    • Sitecore.FieldIDs: a static class for holding well-known GUIDs relating to template fields, useful for API work on Templates
    • Sitecore.ItemIDs: a static class for holding assorted well-known GUIDs, for example the ID of the Layouts root folder or the Templates root folder
    • Sitecore.MainUtil: far too broad to describe in detail but here’s a few functions included you may have coded yourself in previous Sitecore projects: ColorToString, CombineArrays, DecodeName, EncodeName, SendMail (uses SMTP settings from the web.config).
    • Sitecore.StringUtil: utility functions for string manipulation
    • Sitecore.TemplateIDs: (really, really useful) a static class for holding well-known GUIDs relating to core Sitecore templates, i.e. Command, Folder, Image, MediaFolder and many more.

    Details of all these classes and more are available through SDN, especially useful in Windows Help format for the entire API as part of the documentation package.

Finally, just to continue the private jokes with Igloo, Julian has been kind enough to wait for us to do something on the shared server “when you’re finished playing Microsoft Flight Simulator”, and I’ve told him repeatedly that it’s actually The Sims we’re all hooked on. The pool trick I told Julian about last week, well I found the page where I learnt about it as a kid in my late teens (with no friends). On Sammys Kill your Sim tutorial (possibly the greatest title in SEO history) the example you see below has not been photoshopped.

 

Nice ass Jules

This is what will happen if Igloo commit code that breaks the build

LinkedInDiggRedditTumblrGoogle GmailPrintFriendlyEmailShare

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. ;)

LinkedInDiggRedditTumblrGoogle GmailPrintFriendlyEmailShare

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.

LinkedInDiggRedditTumblrGoogle GmailPrintFriendlyEmailShare

Switch to our mobile site