This week I needed to create an Multilist which required a DataSource that doesn’t reside in Sitecore.
I’ve done this a few times before but using an DataSource for a Multilist from outside of Sitecore isn’t something I have needed to do often.
There aren’t many articles on how to do this, so I thought I’d write up how to do this using Sitecore 9 (it should work with Sitecore 8 and below too).
I’ve based the implementation on the code from this post by Benjamin Vangansewinkel: http://sitecoreblog.blogspot.co.uk/2012/04/how-to-create-custom-multilist-field.html. So Kudos to Benjamin for writing this back in 2012. The are alternatives to this approach such as using creating a custom token or a custom data provider but I feel a custom Multilist is simpler and easier to manage.
Benjamin’s post is really useful, but some of the code is missing and I needed to show a count of the items available and selected in my Multilist. I’ve also simplified things a little bit and included the full code with an example API call to populate the list.
When it’s complete it will look like this:
Ok lets get started.
Create a Base Class – BaseExternalMultiList.cs
So that we can re-use this implementation for other Multilists with external DataSources we are going to create a Base Class that encapsulates the code for building the Multilist.
The class provides the method for rendering out the Multilist field and I have updated the Javascript to show a count of the number of available items and the selected items when you add or remove items from the list.
Create An Custom MultiList Field – CryptoMultilist.cs
The next Step is to Create our Multilist field by implementing the Base Class. There are 3 methods here that can be implimented:
- InitRendering() – in this method we will call our API and populate the list.
- GetNonSelectedItems() – in this method we will get our list of all available items.
- GetSelectedItems() – in this method we will populate our list of selected items.
Just for fun I’m using an example of a Multilist field that lists all available Crypto Currencies. I’m using this API to get the list: https://www.cryptocompare.com/api
The API Service – CryptoService.cs
You can obviously use any Service but this example makes a simple call to get a list of Crypto Coins. There are a lot of them apparently, 2528 at time of writing!. This is not production-ready code but provides a simple example for testing. Replace this service with any other service you like to return a list of items.
Supporting Classes
There are some supporting model classes required by the above Service code that allow me to Serialize the JSON response from the API to these models. I’m using Json.Net to do this. Depending on the API you are using you will need to update these models as required.
Once you have added the above code to your project you can then move on to configuring Sitecore to use your custom Multilist field.
Adding the Custom Multilist to Sitecore
Go to the Core database in Content Editor and navigate to:
/sitecore/system/Field types
Create a new item called ‘Crypto Multilist’ using this template:
/sitecore/templates/System/Templates/Template field type
It is best practice to create a new folder to hold your custom field types (if you don’t have one already).
Add the Assembly and Class information (based on the namespace and assembly of the files you have added). Add an icon to identify your field easier also.
Save your new field and then go back to the Master Database in Content editor and navigate to a template you wish to use the new Multilist field on.
Using your Multilist Field
You should now be able to add your new Multilist field to any Data template by selecting it as a Field Type from the dropdown.
Now when you add or edit an item using this Data Template your Multilist field should display populated with the External data and maintain the selected items when saving and going back to the item.
Thats all there is to it, you can now re-use this approach for any additional External Datasources you wish without much effort by switching out the service and models.
I might package this up and put it on NuGet at some point if it’s useful. I’d be interested to know of easier or better ways of doing this also.
Adam – cool post and subject matter. 🙂
Glad you liked it Dan. I’ve got a follow up on a custom validation for this too and i’d like to see if it’s possible to make the Multilist with Search work with an External Datasource too. I doubt it though as it requires Items from a bucket I think.