Derek Hunziker
Derek Hunziker

Reputation: 13141

How to get current Field Name in a custom field type

I'm attempting to retrieve the current field name on a custom Sitecore field I'm building. In other words, if my custom field type is applied to a field named "Meta title", I need to be able to retrieve that name.

This tutorial implies that all you have to do is create a public auto-property called FieldName and the rest is history... sadly, it doesn't appear to work (FieldName is always null or empty).

Any ideas? I've tried inheriting from Sitecore.Web.UI.HtmlControls.Control and Sitecore.Shell.Applications.ContentEditor.Text with no luck.

Upvotes: 2

Views: 1365

Answers (1)

nickwesselman
nickwesselman

Reputation: 6890

You should have access to the Field ID in your control by creating a FieldID property. This should equate to the ID of the Template Field item that defines the field. Thus you can get the name of the field by

var fieldItem = Sitecore.Context.ContentDatabase.GetItem(this.FieldID);
var fieldName = fieldItem.Name;

The full list of properties that Sitecore may set on your control can be found in Sitecore.Shell.Applications.ContentEditor.EditorFormatter.SetProperties.

ReflectionUtil.SetProperty(editor, "ID", field.ControlID);
ReflectionUtil.SetProperty(editor, "ItemID", field.ItemField.Item.ID.ToString());
ReflectionUtil.SetProperty(editor, "ItemVersion", field.ItemField.Item.Version.ToString());
ReflectionUtil.SetProperty(editor, "ItemLanguage", field.ItemField.Item.Language.ToString());
ReflectionUtil.SetProperty(editor, "FieldID", field.ItemField.ID.ToString());
ReflectionUtil.SetProperty(editor, "Source", field.ItemField.Source);
ReflectionUtil.SetProperty(editor, "ReadOnly", readOnly);
ReflectionUtil.SetProperty(editor, "Disabled", readOnly);

Upvotes: 4

Related Questions