Sitecore: extending field datasource queries with tokens,

It is quite common situation when you need to create data source query for a field in a template that is quite complex: e.g you would like to use current item property as a part of a query or you have dependency on some other item in a content tree.

In case of list fields you could use GetLookupSourceItems pipelines to modify query according to your requirements.

namespace IExamples.Sc.Pipelines.GetLookupSourceItems
{
    using System.Linq;
 
    using Sitecore.Data;
    using Sitecore.Data.Fields;
    using Sitecore.Data.Items;
    using Sitecore.Diagnostics;
    using Sitecore.Pipelines.GetLookupSourceItems;
 
    public class DataSourceTokens
    {
        public void Process(GetLookupSourceItemsArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
 
            // Custom code to get value of dependency
            var dependency = "some value";
            
            // set default values for tokens
            var token = dependency ?? "."
            
            // replace tokens in datasource of the field
            args.Source = args.Source
                .Replace("{{token}}", token);
        }
    }
}

As usual you will need to register this pipeline in sitecore patch file and put it in /app_config/include

Finally, to use it need to use defined token in a field query, when you defining your template.

Simple, but give you a lot of flexibility.

Sitecore: extending field datasource queries with tokens,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s