Having code as your field its datasource
January 10, 2017
1

Recently I ran into the problem that i had a MultiList field which i had to populate with items  which had a list in which the MultiList  item was referenced. This was not easy to do using Sitecore queries so i had already decided to create a custom field which could do this in code for me and to start with that I dove into the code behind the MultiList field. To my surprise i found a far better way to fix my issues than with building a custom field: i can use a piece of code to fill my multilists options!

What did I discover?

After diving into the code I found out that fields like the MultiList field(also CheckList, TreeList and other lists referencing items) in the end use the “getLookupSourceItems” pipeline. The great thing around this pipeline is that it implements the “Sitecore.Buckets.FieldTypes.CustomDatasource” processor and this enables us to use code as our fields datasource. This happens because this processor checks for the “code:” keyword and if it is there it will execute the custom code specified.

How to get started with code as a datasource?

To get started with using code as a datasource there is one prerequisite:

  • Have the “Sitecore.Buckets.dll” referenced in your project, preferably do this using Sitecores nuget feed.

Now to create a multilist having code as its source follow the following steps

  1. Start with creating a new C# class implementing the IDatasource interface (Sitecore.Buckets.FieldTypes.IDatasource):
     
    using System;
    using Sitecore.Buckets.FieldTypes;
    using Sitecore.Data.Items;
    
    namespace Blog.CodedDatasource
    {
      public class Example:IDataSource
      {
        public Item[] ListQuery(Item item)
        {
          throw new NotImplementedException();
        }
      }
    }
    
  2. Now put any code in this new class its “ListQuery” method as long as it returns an array of items:
    using System;
    using Sitecore.Buckets.FieldTypes;
    using Sitecore.Data.Items;
    
    namespace Blog.CodedDatasource
    {
      public class Example : IDataSource
      {
        public Item[] ListQuery(Item item)
        {
          var root = Sitecore.Context.ContentDatabase.GetItem("/sitecore/content");
    
          return root.Children.ToArray();
        }
      }
    }
    

    The code example above is a very simple one to get the point accross, do not ever use this in a live project

  3. Create a template having a MultiList field:
    Template.png
  4. Make sure the MultiList field has a source starting with “code:” and then followed by “This.Is.My.Full.Namespace.And.Class, This.Is.My.Dll” in my case this is “Blog.CodedDatasource.Example, Blog.CodedDatasource”
  5. Now create an instance of your template and see the result:
    live.png

TL;DR

Sitecore offers you a way to allow code as a datasource for some field types like MultiList,CheckList and TreeList without doing any customisations. It is as simple as using the “code:” keyword and a class implementing the Sitecore.Buckets.FieldTypes.IDatasource interface. This allows for a lot of flexibility in the source of a field but it could also introduce lot of extra work if you would do this for every field. It is a great solution for complex situations but for simpler situations my recommendation is to stick with Sitecore queries or static paths.