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 <web root>/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.

No comments :

Post a Comment