In this blog I will describe how to create a very simple Custom Text field in Sitecore. I have based this off the example of a custom checkbox field that can be found in the Sitecore website from here.
I have found that this example is far too complicated. I tried to create a Custom control by inheriting Sitecore.Web.UI.HtmlControls.Control but I could not get it working properly initially (I find the Custom check box control that Sitecore has used as an example is far too complicated) I was sent a better example by sitecore and I will make the same custom control by inheriting this class in the near future when I find the time. However, because of current time constraints I had to take an alternative approach.
The control I have made is inheriting Sitecore.Shell.Applications.ContentEditor.Text directly and then overriding the HandleMessage event.
I needed to create a Custom Text field which behaves the same way as Sitecore’s Text field but I needed some extra menu items. Basically I needed to allow my authors to add some extra text Macron characters e.g. Ā
Here is the Sitecore Field Type item I have created (this should be implemented following the Sitecore example) Base Template is Sitecore>Templates>System>Templates>Template Field Type:

This is the Macron menu item I will be using as an example:

This is the end result I am trying to achieve (A regular text box with extra drop down menu items in Authoring mode):

Here is the Code MacroText.cs. As you may notice only the Clear field and the actual macron menu functions are currently working:
Note: I also had to chuck a space where it says ‘& #256;’ between the & and the #256; because otherwise this blog seems to keep converting it to “Ā’ within the code!!
I have uploaded this code to another blog that actually retains the indentation here
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;
using System.Web.UI;
namespace Datacom.FieldTypeExtensions
{
class MacroText : Sitecore.Shell.Applications.ContentEditor.Text
{
#region Constructor
public MacroText()
{
this.Class = “scContentControl”;
base.Activation = true;
}
#endregion
#region Overrides
public override void HandleMessage(Sitecore.Web.UI.Sheer.Message message)
{
if (message["id"] == this.ID)
{
string messageText;
if ((messageText = message.Name) == null)
{
return;
}
switch (messageText)
{
case “macrotext:copy” :
break;
case “macrotext:paste”:
break;
case “macrotext:clearfield”:
this.Value = String.Empty;
break;
case “macrotext:capitalAMacro”:
this.Value = this.Value + “& #256;”;
break;
}
}
base.HandleMessage(message);
}
#endregion
}
}
Here is my Sitecore web.config entry (the name of my assembly is ‘Datacom.FieldTypeExtensions’, this can be edited via the properties of your .NET project as can be seen in next image below):
<controlSources>
<source mode=”on” namespace=”Datacom.FieldTypeExtensions” assembly=”Datacom.FieldTypeExtensions” prefix=”fieldTypeExtension”/>
Hopefully someone finds this helpful!
February 4, 2008 at 7:08 am |
Hey Richard,
Welcome to the (Sitecore) blogsphere.
This is great, – thans for posting this cool simplified example. And you are right, our example on the developer network may be a bit too complex for first-time custom field developers.
Anyway, – I hope to see more exiting blog posts from New Zealand. I also saw James Frasers interesting posts and are very excited about the competences we see from this region.
Do you plan other posts?
Best,
Lars Fløe Nielsen
February 4, 2008 at 11:07 pm |
Hi Lars,
Yes I will most certainly be adding a few more posts on Sitecore. We are going to be very busy days working on full on Sitecore projects.
Cheers,
Richard
May 21, 2009 at 8:35 pm |
Hi Richard, I’m trying to create a custom dropdownlist in Sitecore 6 and am unsure of what base class to use. Big thing is we want to pull in name/value pairs from a separate db and populate a dropdown for a content type in sitecore.
Any direction would be greatly appreciated.
Thanks,
Jim
May 22, 2009 at 10:06 am |
Hi Jim,
I am assuming you will only need to select one value from the drop down? if that is the case I would suggest you use a DropList type. Refer to the Data Definition Reference Section 4.2 if you have any doubts.
Use Reflector to take a look at the following classes:
Sitecore.Shell.Applications.ContentEditor.LookupEx
I believe you should override its DoRender() method which is currently:
protected override void DoRender(HtmlTextWriter output)
{
Assert.ArgumentNotNull(output, "output");
Item current = Context.ContentDatabase.GetItem(this.ItemID);
Item[] items = this.GetItems(current);
output.Write("");
output.Write("");
bool flag = false;
foreach (Item item2 in items)
{
string itemHeader = this.GetItemHeader(item2);
bool flag2 = this.IsSelected(item2);
if (flag2)
{
flag = true;
}
output.Write("" + itemHeader + "");
}
bool flag3 = !string.IsNullOrEmpty(this.Value) && !flag;
if (flag3)
{
output.Write("");
output.Write("" + this.Value + "");
output.Write("");
}
output.Write("");
if (flag3)
{
output.Write("{0}", Translate.Text("The field contains a value that is not in the selection list."));
}
}
You might also want to look at Sitecore.Shell.Applications.ContentEditor.Lookup . ..
Unfortunately our databases are down at the moment and I do not have access to Sitecore in order to check exactly which Sitecore 6’s current DropLink field references.
It is quite obvious though that you will have to replace the code for Item[] items = this.GetItems(current); above with the dropdown values you have obtained via your 3rd party DB. . . . best of luck, let me know how it goes.