Umbraco/Create xslt exstension like umbraco.Library in C

Create a new xslt extension like umbraco.library in C#.

Sometimes you need more functionality in your xslt, and most of the time umbraco.Library is enough. But what do you do if that isn’t enough?

There are 2 ways to create your own functions

1. Inline code. 2. xslt extension

My opinions.

Inline code.
Inline code get my xslt look messy, and are difficult to reuse, but works fine if you only need a single function. More info about inline code can be found here

xslt extension.
xslt extension on the other hand looks much cleaner, and is easy to re/use again.

Step by Step Ill go through it step by step

1. Create a class library, in my case “ronnie.library”.
2. Create your class/es that you need. In my case “dateMethods.cs”
3. Create the methods you need (remember the methods have to be public and static) e.g.

  public static int ugeNummer(DateTime dato)
  {
  string res;
  res = Microsoft.VisualBasic.DateAndTime.DatePart(Microsoft.VisualBasic.DateInterval.WeekOfYear, dato,       
  Microsoft.VisualBasic.FirstDayOfWeek.Monday, Microsoft.VisualBasic.FirstWeekOfYear.System).ToString(); 
  return Int32.Parse(res); 
  }


4. When you are done creating the methods, build and copy the dll in my case “ronnie.library.dll” into the bin folder.

5. Now you just have to register your xslt extension, and this is done in xsltExtensions.xml (placed in the config folder).

6. Open the file and add the following line. Note that starting with Umbraco 4.5, /bin/ is not needed anymore!

<ext assembly="/bin/ronnie.library" type="ronnie.dateMethods" alias="CoolDateMethods" />
Assembly
here you type where you have placed your dll file, (without the .dll extension)
type
".NET_namespace.ClassName” Here you have to write the namespace followed by a dot and the class you want to use.
alias
This is your xmlns like umbraco.Library, call it what you like. In my case CoolDateMethods


7. Last but not least you have to remember to add the xmlns in your xslt document; this is done like this:

xmlns:CoolDateMethods ="urn:CoolDateMethods"
exclude-result-prefixes="msxml umbraco.library CoolDateMethods


Now you should be ready to use your new xslt extension. Hope this quick’n dirty article was informative for you.

//Ronnie