I have a source query over a standard Multi-List field and I want the query to retrieve items based on the current selected language.
Is there any attribute that could handle that in the source query?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityOOTB Sitecore, this is not possible using Sitecore queries because Sitecore does not take into account which language has been selected.
Get Lookup Source Item
You can use the Lookup Source Item pipeline (<getLookupSourceItems>
). This particular pipeline is responsible to execute the Sitecore query you've filled in the Datasource Field. Moreover, you can implement your business logic to retrieve items.
Implementation
You will need to implement a method which has GetLookupSourceItemsArgs
as parameter. The args
paramter has a property named Item which contain the selected item in the current selected version.
public void Process(GetLookupSourceItemsArgs args)
{
if (this.SkipProcessor(args)) return;
var items = Factory.GetDatabase("master").GetItem("PATH TO CONTAINER ITEM OR SITECORE ID"), args.Item.Language).GetChildren(ChildListOptions.SkipSorting);
foreach (var item in items)
{
var itemVersions = item.Versions.GetVersions();
foreach (var itemVersion in itemVersions)
{
if (itemVersion.Language.Name.Equals(args.Item.Language.Name))
{
args.Result.Add(itemVersion);
}
}
}
args.AbortPipeline();
}
private bool SkipProcessor(GetLookupSourceItemsArgs args)
{
return !args.Source.ToLower().Contains("itemversionspecific");
}
Configuration File
Once you implemented the code, you need to add it to the getLookupSourceItems
pipeline. Below is an example of the patch.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<getLookupSourceItems>
<processor type="YourNamespace,YourAssembly" patch:before="*"/>
</getLookupSourceItems>
</pipelines>
</sitecore>
</configuration>
Testing
Based on the above code, I tested it by creating a container item named Items
holding 3 items
Upon selection of a language version, the items from the Multi-List Field changes the list of items.
Multilist
or aMultilist with Search
?