Here at BrainJocks we have recently built two custom field types for Sitecore – Geo Location and YouTube Video. Both fields are “picker” fields with custom controls for Content Editor and Page Editor. Sitecore Marketplace has a few relevant downloads (at least for the location picker) but neither looked complete and good enough.
Building a custom field type with support for Content and Page Editor isn’t particularly hard (when you did it more than once and worked through all the gotchas, that is). That said, I don’t believe there’s a comprehensive end to end guide out there on how to do it and the documentation on all the moving parts involved in the process is rather scarce. Hopefully, as SPEAK matures and MVC gets deeper into the Sitecore core, this guide will become less and less relevant. In the meantime, however, I hope that you will find it useful.
As I post it I will share the code on github and will also publish a complete solution as a module on Sitecore Marketplace.
Here’s a list of things that need to come together for the YouTube Video Picker to be a complete* solution:
All but #4 (and
DoRender() of #3) are rather mechanical and will be largely the same for your custom fields. It all will be of course flavored with the specifics of what exactly the field represents but other than that it’s the means by which you make Sitecore aware of your field’s existence and teach your field to be a good Sitecore citizen. The XML (or XAML) control (and the presentation part of the Content Editor control) on the other hand is unique to each custom field. It has to be aware of how Sitecore digests and sandboxes it but other than that you can pretty much do anything you want with it. It is is your playground. Everything else is rules and laws that you have to know, respect and abide by.
- Sitecore 7.0 rev. rev. 130918
- VS 2012
- TDS 4.0
Let’s Get To It!
First off, the
raw value of our field. We have two options – a string literal or a XML structure. Stick with the literal if you can. A list can be pipe separated. A tuple can be coma separated. For the YouTube Video all we need to store is the ID so literal it is.
Next comes the standalone prototype. It’s way faster to iterate on and test your HTML and JS outside of Sitecore. The Content Editor control is a WebControl so it will have to render all markup directly. XML dialog control gets compiled into C# code and then renders a somewhat different markup that in turn gets injected into a wrapping dialog structures. We will take a very close look at that later but trust me – we are much better off kicking the Sitecore tires with a working and tested prototype.
Interested? Part 2 will cover all the upfront prototyping. Stay tuned!
* I don’t mean to say that the field has all the features. YouTube Data API alone has tons of things it can do. For this series I measure completeness in terms of how complete the implementation is in terms of its integration with Sitecore. Does it nicely support Content Editor? Does it also work as nice in Page Editor? Is it friendly to DMS?