Selecting Dynamic Content in Native Sitefinity Modules with Custom Fields

The Sitefinity Module builder allows you to create custom types on the fly for use as custom modules.

In the 5.0 release back in February, a two new field types were added to the module builder: Guid and Array of Guids. These allow you to “link” content items using their Id properties, adding support of one-to-many and many-to-many relationships between your content.

Radoslav posted a helpful sample of a custom dynamic content selector field control that allows you to select content for these relationships. While this sample is thorough, it is a lot of custom code to work with if you simply want to make a basic association.

Such a situation occurs with the recently released Sitefinity MVC Store sample project. In this project, there are two content types Artist and Genre that are referenced in a one-to-many relationship to the content type Album. Instead of implementing the full selector above, a simpler control was created by inheriting from the existing ChoiceField control.

This field already contains a lot of the functionality required, since it was designed to present a list of options to the user, in many forms. In this case, the field is defaulted to a dropdown list:


In this example, the name of the field (Artist and Genre exactly match the name of the Content Type being associated. This eliminates the need to create a separate field for each content type, and it can even be reused in other content modules.

Here is the full code for this custom field control, showing how this was achieved.

View Code Sample on Gist

Keeping the Default Behavior

Notice that not only does this control inherit from ChoiceField but the ScriptDescriptorType and ResourcesAssemblyInfo overrides also force this control to reuse the base behavior of that field. The reason for this is that by doing so, the control will reuse the native JavaScript file for the designer, so that it isn’t necessary rewrite a entirely new script from scratch just to accommodate our custom type. Instead, the default one for ChoiceField is compatible since this control inherits directly from it.

Defining the Desired Behavior

Also, you can see that the RenderChoicesAs property forces a list. This could be modified if you a different input type is required. However, using a custom field does not present the option to set this on the website when the field is added, so it is explicitly set here.

Populating the Choice Collection

Finally, the Configure method takes care of actually loading the choices and presenting them to the user. While you can certainly hard-code your custom module and content types into the control, the FieldDefinition that is passed to this method includes a lot of information that can be used to extract the data needed to load the content items.

The ControlDefinitionName contains the type of the current content type using dot notation (Example: Telerik.Sitefinity.DynamicTypes.Model.MusicStore.Album), so removing the last element results in the parent module type.

NOTE: This selector does not account for nested or child types, and should be modified if you are using such a content type.

The name of the field is stored in the FieldName property, and appending them together results in the full name of the dynamic content type.

This name can then be used to initialize the Content Type required by the Dynamic Module Manager to retrieve content items of that type. From there a simple use of the Sitefinity API retrieves the published items and binds them to the Choices collection so they can be shown in the dropdown list.


Retrieving Associated Items Using the API

Once your content items are added with these associations using the new custom field, you can easily retrieve them back using the API. This is demonstrated in the Browse method of the StoreController.cs class:
View Code Sample on Gist

Which reveals the albums which match the selected Genre:


Another Year Another Upgrade

So much has happened since I last (again) stopped updating this blog. I’ve been granted the rare and fantastic opportunity to join the team at Falafel Software as a developer. I’m seriously having the time of my life working on exciting new projects and even new platforms like Windows 8 and Windows Phone!

As a result, however, I did need to make a few life changes. The most important being that I have suspended my freelance services indefinitely (hopefully permanently!) as long as I continue to work with the Falafel team. That time was certainly a fun ride, and I gained a ton of experience that has been instrumental in getting me where I am today! But now I’m happy to be a part of this fantastic team, and dedicate 100% of my time to Falafel and its customers.

In addition, I’ve also folded my Sitefinity Guru blog back into my personal blog, and am going to be shuttering that site, blog, and video series. The existing posts will remain, but since I want to channel all my writings through Falafel, all my new Sitefinity and other software related posts will now be posted on the Falafel Blogs. Specifically you can follow my blog posts here.

This personal blog will remain mostly as a sounding board for my rants (like my recent issues with Nokia and Windows Phone, which by the way STILL continue to vex me), as well as anything worth announcing (like new side projects and music). In addition, you can always find me on Twitter at @SelAromDotNet.

I’d like to thank any readers who found my posts helpful, and I hope you will continue to follow my updates on the Falafel Blogs. If anyone is interested in taking over the SitefinityGuru domain and Twitter account, please let me know, otherwise I’ll just keep it to redirect the old posts into my site.

Until next time (next year probably!) thanks for reading.