Cant seem to get custom UmbracoTreeSearcherFields to work

I’ve created a custom UmbracoTreeSearcherFields class to add some extra fields into the backoffice search functionality. The search doesn’t seem to be using them, so I added some extra logging into the class, but I can’t see this either.

Have I setup the new fields correctly, and is this the correct way to register it?

My composer:

builder.Services.AddUnique<IUmbracoTreeSearcherFields, CustomUmbracoTreeSearcherFields>();

My class:

public class CustomUmbracoTreeSearcherFields : UmbracoTreeSearcherFields, IUmbracoTreeSearcherFields
    {
        private readonly ILogger<CustomUmbracoTreeSearcherFields> _logger;
        private readonly IServiceScopeFactory _serviceScopeFactory;
        
        public CustomUmbracoTreeSearcherFields(
            ILogger<CustomUmbracoTreeSearcherFields> logger,
            ILocalizationService localizationService,
            IServiceScopeFactory serviceScopeFactory) : base(localizationService)
        {
            _logger = logger;
            _serviceScopeFactory = serviceScopeFactory;
            _logger.LogWarning("CustomUmbracoTreeSearcherFields instantiated");
        }
        
        public override IEnumerable<string> GetBackOfficeDocumentFields()
        {
            _logger.LogInformation("Getting backOffice document fields");
            
            var fields = base.GetBackOfficeDocumentFields().ToList();

            using var scope = _serviceScopeFactory.CreateScope();
            var publishedContentTypeCache = scope.ServiceProvider.GetRequiredService<IPublishedContentTypeCache>();

            fields.Add($"{Constants.Examine.Searchable}_{Constants.Examine.Index.UniqueMemberId}");
            fields.Add($"{Constants.Examine.Searchable}_{Constants.Examine.Index.MemberGuid}");

            fields.Add(Device.GetModelPropertyType(publishedContentTypeCache, x => x.UniqueBoardID).Alias);
            fields.Add(Device.GetModelPropertyType(publishedContentTypeCache, x => x.SerialNumber).Alias);

            fields.Add(GaitSession.GetModelPropertyType(publishedContentTypeCache, x => x.DeviceSerialNumber).Alias);
            fields.Add(GaitSession.GetModelPropertyType(publishedContentTypeCache, x => x.SessionName).Alias);

            foreach (var field in fields)
            {
                _logger.LogInformation("Added field to search: {Field}", field);
            }

            return fields;
        }
        
        //Adding custom field to search in members
        public override IEnumerable<string> GetBackOfficeMembersFields()
        {
            var fields = base.GetBackOfficeMembersFields().ToList();

            using var scope = _serviceScopeFactory.CreateScope();
            var publishedContentTypeCache = scope.ServiceProvider.GetRequiredService<IPublishedContentTypeCache>();

            fields.Add(VirtualMember.GetModelPropertyType(publishedContentTypeCache, x => x.MemberId).Alias);

            return fields;
        }
    }

Hi @redmorello

That’s what the docs say:

builder.Services.AddUnique<IUmbracoTreeSearcherFields, CustomUmbracoTreeSearcherFields>();

Does your code get called if you debug through it?

Justin

Ah, I think I’ve misunderstood which search I’m interacting with.

My custom fields ARE being used, but only once I start a search from the main search box (top right, green arrow)

What I was trying to do, was add my search fields into the Collection filter at the top of the collection (red arrow). In V13 I achieved this using a custom ContentController and overriding the GetChildren method. I’ve added the extra fields into the Collection view, as I can see them, but the filtering doesn’t seem to use them?

Thanks for clarifying.

I can’t find anything out of the box that allows you to extend the collection search…

Yeah, not without switching out the default collection view for your own workspace, with all the extra code that involves :roll_eyes:

1 Like