3

We are trying to add dynamic fields in a bucket search results view. We want to display custom fields. In the previous versions of sitecore there use to be a DynamicFields pipeline. As stated in the sitecore 9 release notes, the pipeline has been removed.

How can we achieve this using sitecore 9?

1
  • This should be a comment but the button is disabled due to too low reputation (seriously who thought that was a good idea?). I want to point out that Soren Kruse's answer doesn't seem to work anymore in sitecore 9.2. You will get the following error: ``` System.InvalidOperationException: 'Could not find method: Process. Pipeline: /sitecore[database="SqlServer" xmlns:patch="sitecore.net/xmlconfig/"]/pipelines/…, Sitecore.Buckets"]' ``` So it
    – Barsonax
    Commented Oct 28, 2019 at 15:55

2 Answers 2

5

We had the same issue, luckily it isn't too hard to re-implement. I believe this should work exactly like before, but we might have skipped something we didn't need so just be aware of that.

AddDynamicFields.cs

using System.Collections.Generic;
using System.Linq;
using Sitecore;
using Sitecore.Abstractions;
using Sitecore.Buckets.Pipelines.UI.FillItem;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.Data.Items;
using Sitecore.Globalization;
using YourSolution.DynamicFields

namespace YourSolution
{
    public class AddDynamicFields : FillItemProcessor
    {
        public override void Process(FillItemArgs args)
        {
            foreach (var searchResultItem in args.ResultItems.OfType<SitecoreUISearchResultItem>())
            {
                Language itemLanguage;
                Language.TryParse(searchResultItem.Language, out itemLanguage);

                var version = Sitecore.Data.Version.Parse(searchResultItem.Version);

                var item =
                    Context.ContentDatabase.GetItem(searchResultItem.ItemId, itemLanguage, version) ??
                    Context.Database.GetItem(searchResultItem.ItemId, itemLanguage, version);

                if (item == null)
                    return;

                LoadDynamicFields(item, searchResultItem);
            }
        }

        private static void LoadDynamicFields(Item innerItem, SitecoreUISearchResultItem sitecoreItem)
        {
            if (sitecoreItem.DynamicFields == null)
                sitecoreItem.DynamicFields = new List<KeyValuePair<string, string>>();

            var dynamicFieldsArgs = new DynamicFieldsArgs { InnerItem = innerItem};
            var corePipelineManager = ContentSearchManager.Locator.GetInstance<BaseCorePipelineManager>();
            corePipelineManager.Run("buckets.dynamicFields", dynamicFieldsArgs);

            sitecoreItem.DynamicFields = dynamicFieldsArgs.DynamicFields.ToList();
        }
    }
}

DynamicFieldsArgs.cs

using System.Collections.Generic;
using Sitecore.Buckets.Pipelines;
using Sitecore.Data.Items;

namespace YourSolution.DynamicFields
{
    public class DynamicFieldsArgs : BucketsPipelineArgs
    {
        public Item InnerItem { get; set; }

        public IDictionary<string, string> DynamicFields { get; set; } = new Dictionary<string, string>();
    }
}

DynamicFieldsProcessor.cs

using Sitecore.Buckets.Pipelines;

namespace YourSolution.DynamicFields
{
    public abstract class DynamicFieldsProcessor : BucketsPipelineProcessor<DynamicFieldsArgs>
    {
    }
}

Config patch

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <pipelines>
      <buckets.fillItem>
        <processor type="YourSolution.AddDynamicFields, YourSolution" />
      </buckets.fillItem>
    </pipelines>
  </sitecore>
</configuration>
0
0

The problem in Soren Kruse's answer is that he is defining a custom DynamicFieldsArgs class. The processor that sitecore uses uses a different class (but with the same name) so this will break. The fix is to just use the class that sitecore gives you.

I modified Soren Kruse's answer for my own needs where I just wanted to show values from the index itself (currently its loading all fields which might be a bit much though):

    public class AddDynamicFields : FillItemProcessor
    {
        public override void Process(FillItemArgs args)
        {
            foreach (var searchResultItem in args.ResultItems.OfType<SitecoreUISearchResultItem>())
            {
                var dynamicFields = new List<KeyValuePair<string, string>>();

                foreach (var field in searchResultItem.Fields)
                {
                    dynamicFields.Add(new KeyValuePair<string, string>(field.Key, field.Value?.ToString()));
                }

                searchResultItem.DynamicFields = dynamicFields;
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.