Tuesday, June 21, 2011

Custom macro datatype for macro properties - Umbraco

In this post i will teach you how to create a dropdown list for selecting value for Umbraco Macro Properties.Usually for Macro properties we can set only few predefined datatype while adding properties in umbraco admin. In order to have our custom datatype we need to edit Umbraco source. Open Umbraco source code in Visual Studio. Add a new class to umbraco.macroRenderings project with the following code.

 


using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
namespace  umbraco.macroRenderings
{
class  HotelsDropdown  : System.Web.UI.WebControls.DropDownList , umbraco.interfaces.IMacroGuiRendering     
{
string  _value = string .Empty;
public  string  Value
{
get  {
string  retVal = string .Empty;
foreach  (System.Web.UI.WebControls.ListItem  i in  base .Items)
{
if  (i.Selected == true )
{
retVal = i.Value;
}
}
return  retVal;
}
set  { _value = value ; }
}
public  bool  ShowCaption
{
get  { return  true ; }
}
protected  override  void  OnInit(EventArgs  e)
{
base .OnInit(e);
for  (int  i = 0; i < 5; i++)
{
System.Web.UI.WebControls.ListItem  li = new  System.Web.UI.WebControls.ListItem ("test"  + i.ToString());
if  (_value == "test"  + i.ToString())
li.Selected = true ;
this .Items.Add(li);
}
}
}
}

compile and copy .dll to umbraco bin.

add macro class info to umbraco "cmsMacroPropertyType" table:

Click image to enlarge


add new object type to your macro:
Click image to enlarge

add/embed macro in page or template and set macro property values using the our Dropdown:

Click image to enlarge


Output in editor:

Click image to enlarge

Output in browser:

Click image to enlarge


No comments:

Post a Comment