Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dnstommy
Created March 18, 2019 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnstommy/8372c785b1916900caee2a3d75ca0e34 to your computer and use it in GitHub Desktop.
Save dnstommy/8372c785b1916900caee2a3d75ca0e34 to your computer and use it in GitHub Desktop.

I don't think it is possible in query language to filter out duplicates. But you could use a custom getLookupSourceItems processor and just do the work yourself. The 1st thing to do would be to create a custom token so that your code knows to look for it. Like change query to splitQuery. That would give you a field datasource path like this:

dedupeQuery:/sitecore/content/All Organizations/Photos//*[@@templateid='{55A41A27-A674-4A04-9716-8E5CB8786A85}']|/sitecore/content/All Organizations/Photos//*[@@templateid='{55A41A27-A674-4A04-9716-8E5CB8786A85}']

Then in the processor will look for dedupeQuery and remove it. Then it will query the db for it.

The processor

using System.Collections.Generic;
using System.Linq;
using Sitecore.Data.Items;
using Sitecore.Pipelines.GetLookupSourceItems;

public void Process(GetLookupSourceItemsArgs args)
{
    //if (!args.Source.StartsWith(Constants.StyleDataSource.StyleToken)) return;

    if (!args.Source.StartsWith("dedupeQuery:")) return;

    var query = args.Source;

    // remove the dedupeQuery
    query = query.Replace("dedupeQuery:", "");

    // get our DB
    var web = Sitecore.Configuration.Factory.GetDatabase("web");

    // execute our query
    var results = web.SelectItems(query);

    // remove the dupes
    results = results.GroupBy(x => x.ID).Select(y => y.First()).ToArray();

    args.Result.AddRange(results);

    // this is important; otherwise our args.Result could be changed 
    // by another pipeline step
    args.AbortPipeline();
}

The config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <pipelines>
      <getLookupSourceItems>
        <processor type="Sitecore.Foundation.Theming.CodedDataSource.GetStyleSourceItems, Sitecore.Foundation.Theming" patch:before="processor[@type='Sitecore.Pipelines.GetLookupSourceItems.ProcessQuerySource, Sitecore.Kernel']" resolve="true"/>
      </getLookupSourceItems>
    </pipelines>
  </sitecore>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment