How to link a droplink to a Treelist in Sitecore

吃可爱长大的小学妹 提交于 2019-12-01 11:16:47
Kevin Brechbühl

You can use the getLookupSourceItems-pipeline for this. With a Droplink you can specify a Sitecore query as source. And with the getLookupSourceItems you can change the source at runtime. The following processor checks the items selected in the Treelist and create a Sitecore query which includes all the items selected in the 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));
    }
}

You have to specify which field is your "Source" field within the Droplink source with fromfield=<SourceField>:

At the end you need to configure this pipeline processor:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getLookupSourceItems>
        <processor  patch:before="processor[1]"
                    type="Website.LookupItemsFromField, Website" />
      </getLookupSourceItems>
    </pipelines>
  </sitecore>
</configuration>

I think this is what you are looking for: http://getfishtank.ca/blog/using-item-field-as-a-data-source-in-sitecore

Basically you will be able to set the datasource of one field to another.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!