如何将droplink链接到Sitecore中的Treelist

Tho*_*ens 2 datasource sitecore treelist sitecore7

我试图找出如何将a链接Droplink到a 中的选定项目Treelist.我有一个字段Theme,一个字段,Treelist一个字段MasterTheme,即Droplink.

我应该能够在中选择一个主主题Droplink,其中填充了所选的数据Treelist.

我是Sitecore的新手,我不熟悉Custom类.

Kev*_*ühl 10

你可以使用getLookupSourceItems-pipeline.使用a,Droplink您可以将Sitecore查询指定为源.并且getLookupSourceItems您可以在运行时更改源.以下处理器检查在中选择的项目Treelist并创建Sitecore查询,其中包括在中选择的所有项目Treelist.

public class LookupItemsFromField
{
    private const string FromFieldParam = "fromfield";

    public void Process(GetLookupSourceItemsArgs args)
    {
        // check if "fromfield" is available in the source
        if (!args.Source.Contains(FromFieldParam))
        {
            return;
        }

        // get the field
        var parameters = Sitecore.Web.WebUtil.ParseUrlParameters(args.Source);
        var fieldName = parameters[FromFieldParam];

        // set the source to a query with all items from the other field included
        var items = args.Item[fieldName].Split('|');
        args.Source = this.GetDataSource(items);
    }

    private string GetDataSource(IList<string> items)
    {
        if (!items.Any()) return string.Empty;

        var query = items.Aggregate(string.Empty, (current, itemId) => current + string.Format(" or @@id='{0}'", itemId));
        return string.Format("query://*[{0}]", query.Substring(" or ".Length));
    }
}
Run Code Online (Sandbox Code Playgroud)

你必须指定字段是内你的"来源"字段Droplink源用fromfield=<SourceField>:

在此输入图像描述

最后,您需要配置此管道处理器:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getLookupSourceItems>
        <processor  patch:before="processor[1]"
                    type="Website.LookupItemsFromField, Website" />
      </getLookupSourceItems>
    </pipelines>
  </sitecore>
</configuration>
Run Code Online (Sandbox Code Playgroud)