This forum post triggered me to share some code that I had. A while back I was building a site which let the customers define their own custom overviews One requirement was that users could define the sort by property . To do this you can use the property picker but it wasn’t friendly enough for my case so I ended up building a custom macro parameter type.
To start you need to create a new class library and add references to the following assemblies:
- Businesslogic
- Cms
- Interfaces
- Umbraco
- System.web
In the example below I’ve created a class that inherits from dropdownlist. This was the easiest way since I needed a dropdownlist and didn’t want to play with the control tree, Now more important is that this class inherits from the IMacroGuiRendering Interface. This add two properties ShowCaption, to show the caption on the parameters tab and more important value which holds the selected value .
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using System.Web.UI.WebControls;
5: using umbraco.interfaces;
6:
7: namespace MacroRenderDemo
8: {
9: public class OrderBy : DropDownList, IMacroGuiRendering
10: {
11: protected override void OnLoad(EventArgs e)
12: {
13: base.OnLoad(e);
14: if (this.Items.Count == 0)
15: {
16: this.Items.Add(new ListItem("Title", "nodeName"));
17: this.Items.Add(new ListItem("Date", "createDate"));
18: }
19: }
20:
21: #region IMacroGuiRendering Members
22:
23: public bool ShowCaption
24: {
25: get { return true; }
26: }
27:
28: public string Value
29: {
30: get
31: {
32: return this.SelectedValue;
33: }
34: set
35: {
36: this.SelectedValue = value;
37: }
38: }
39:
40: #endregion
41: }
42: }
When you build the project and add the binary into the bin folder of your Umbraco site you still can’t select the new type from the parameter type list. First we need to register the class in the database. Below you’ll see the database table that holds all the Macro property types.
It is important that you add the Assemblyname including the namespace to the macroPropertyTypeRenderAssembly column and add the name of your class to the macroPropertyTypeRenderAssembly column .
Now we can use the parameter in our macro’s
and off course we can now use the new parameter in our template when we select the macro
Another example how flexible Umbraco is. If a requirement isn’t available out of the box, usuallyall it takes is to implement an interface and write a few lines of code.