How to link a droplink to a Treelist in Sitecore

后端 未结 2 541
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 01:31

I\'m trying to figure out how I can link a Droplink to the selected items in a Treelist. I have a field Theme, which is the Tre

相关标签:
2条回答
  • 2021-01-15 02:30

    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>:

    enter image description here

    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>
    
    0 讨论(0)
  • 2021-01-15 02:33

    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.

    0 讨论(0)
提交回复
热议问题