Share Field types are fields used to build a Sitecore template. This article describes how to create a custom field type based on an existing Sitecore field.

If a certain Sitecore field type (or Mercury field type) doesn't quite meet the necessary requirements, it is possible to inherit one. This requires having a new class in your .NET solution derived from the Sitecore field type. You must add this class to the list of field types in the core database and perhaps also add JavaScript, which handles the client side interaction. Once this is done, you can use the new field type in your templates and items.


1. Creating a new field type class

To be able to create your own class derived from an existing Sitecore field type, you must first find out which Sitecore class is used for the field type you wish to extend. All available field types can be found in the core database under /sitecore/system/Field types, grouped the same way as in the field type selector dropdown that the template builders use.

Now you have established the type you want to derive from, find out which class is used for that specific field type by looking in the Control field (for example, content:Checkbox). The content prefix refers to the Sitecore.Shell.Applications.ContentEditor namespace in the Sitecore.Kernel assembly. The vast majority of all Sitecore field types reside in that namespace.

Note that the prefix can be traced back to the controlSources section in the config. There, you can match them to the related assembly and namespace. You now have the necessary information to create your custom field type class. With this information, you can set up a class in your solution by inheriting the built-in field type.
By using dotPeek (or another decompiler), you can examine which methods you wish to override to achieve the required altered behavior.

2. Exposing the field type

To be able to reference your new field type from Sitecore, you need to create a config (section) in which you expose the namespace, assembly and prefix. An example config follows, based on the sample code above. Example:

3. Create a field type in Sitecore

To be able to use your new field type in Sitecore, it needs to be added in the core database, in the same place you found the field type you are inheriting from: /sitecore/system/Field types. It is recommended you create a separate folder for your field types. They will then also end up in their own section in the dropdown.

The best way to create your field type is to copy the field type you're inheriting from into your folder. After copying it, you need to replace the value in the Control field. The format for this field is <prefix>:<class name>. For our example, this would result in mif:ProductSearchList. You can now start using your newly created field type in your templates.

4. Modify JavaScript (optional)

Some field types use JavaScript for client side interaction, such as searching, moving items from one list to another or formatting strings. This is something you might wish to change as well. If you do so, it is best to copy the original JavaScript file and make your changes.

Keep in mind that the custom field type you created needs to reference the new JavaScript file as well. Again, use a decompiler to find out which method to override. In the example used throughout this article, this method would be the RenderScript method in the Sitecore.Buckets.FieldTypes.SearchList class.

Related articles:

Articles related to this topic

Share a Tweet
Need to create your own custom field type in Sitecore? This article tells you in 4 easy steps #SITECORE # CUSTOM FIELDS #   Share
Field types are fields used to build a Sitecore template. This article describes how to create a custom field type based on an existing Sitecore field #SITECORE # CUSTOM FIELDS  Share