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

Implementaiton

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 the Sitecore patch file and put it in /app_config/include

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

Simple, but give you a lot of flexibility.