Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kalle Wibeck 30 posts 81 karma points
    Feb 15, 2021 @ 14:29
    Kalle Wibeck
    0

    Search in custom member attributes?

    Hi!

    Is it possible to somehow search in custom members properties?

    Say that I've added a custom property "city" can I somehow make this searchable in the great UGuardian table?

    Thanks,

    // Kalle

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Feb 21, 2021 @ 10:03
    Marc Goodson
    0

    Hi Kalle

    By default, the search will only consider the Name and Email fields, however you can extend this to use custom properties by implementing:

    https://our.umbraco.com/documentation/Extending/Backoffice-Search/

    So if you create the following in a c# class and compile it as part of your Umbraco solution, and recycle the application pool, then Umbraco will discover this code at startup and add your custom field to the ones already searched;

    public class CustomUmbracoTreeSearcherFields : UmbracoTreeSearcherFields,     IUmbracoTreeSearcherFields
    {
        public List<string> GetBackOfficeMembersFields()
        {
            var list = base.GetBackOfficeMembersFields().ToList();
            list.Add("aliasOfCustomField");
            return list;
        }
    }
    

    regards

    Marc

  • Dallas 132 posts 404 karma points
    Feb 22, 2021 @ 17:56
    Dallas
    0

    Hi Kalle, Marc - customizing the member search is something that I have been working on too. Our editors need to find members using the custom member properties (in our case, company name and employer id).

    I added a CustomUmbracoTreeSearchFields class to the project similar to Marc's example but was not getting any results when searching on the custom member properties.

     public class CustomUmbracoTreeSearcherFields : UmbracoTreeSearcherFields, IUmbracoTreeSearcherFields
        {
            public new IEnumerable<string> GetBackOfficeMembersFields()
            {
                var list = base.GetBackOfficeMembersFields().ToList();
                list.Add("accountNumber");
                list.Add("employerName");
                return list;
            }
        }
    

    I dug into the Umbraco source and found that the default UmbracoTreeSearchFields class is loaded with the RegisterUnique() method.

    From Stephan's blog post I found that the purpose of RegisterUnique() is to only ever load one instance of the type. I needed to register the custom class in a composer for searching on custom member properties to work.

    public class UmbracoTreeSearcherFieldsComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.RegisterUnique<IUmbracoTreeSearcherFields, CustomUmbracoTreeSearcherFields>();
            }
        }
    

    The other issue I had was that I was using the list search box in the member section to search but this solution applies to the site level search (accessed from the the menu bar). You can search for the members from any section.

    enter image description here

    (Is it possible to customize the member list search? This would be more useful as the results show more details. In the site search you only see the username and email address in the result list.)

    In addition, the indexing needs to be updated to include the custom member properties. By default only username and email are indexed but this can be changed by changing the indexing as described here:

    https://our.umbraco.com/documentation/Reference/Searching/Examine/indexing/

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Feb 22, 2021 @ 18:59
    Marc Goodson
    0

    Hi Dallas

    Thanks for updating further!

    In terms of searching the ListView, this isn't using the Examine Index, it goes straight to the database... however this is a great article here about how you can hijack this request and implement your own backoffice search for a ListView....

    https://dev.to/skttl/how-to-customize-searching-in-umbraco-list-views-1knk

    regards

    Marc

  • Dallas 132 posts 404 karma points
    Feb 22, 2021 @ 19:40
    Dallas
    0

    That's great. Thanks Marc!

  • Gianni 10 posts 80 karma points
    Aug 26, 2021 @ 14:36
    Gianni
    0

    Thanks for your suggest, I tried to implement it into an Umbraco installation. First of all, I changed the "RegisterUnique" with "RegisterUniqueFor" in the Compose method (I'm using Umbraco 8.16.x):

    public class UmbracoTreeSearcherFieldsComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.RegisterUniqueFor<IUmbracoTreeSearcherFields, CustomUmbracoTreeSearcherFields>();
        }
    }
    

    (RegisterUnique is no longer available to use with Service and Target as generics).

    The UmbracoTreeSearcherFields now has a constructor with argument, so I changed in:

    public class CustomUmbracoTreeSearcherFields : UmbracoTreeSearcherFields, IUmbracoTreeSearcherFields
    {
        public CustomUmbracoTreeSearcherFields(LocalizationService localizationService) : base(localizationService) { }
    
        public new IEnumerable<string> GetBackOfficeFields()
        {
            return new List<string>(base.GetBackOfficeFields()) { "myCustomProperty" };
        }
    }
    

    But is not working. Any suggestion?

  • RyanW 33 posts 148 karma points
    Jun 02, 2023 @ 13:36
    RyanW
    0

    Did you ever find a fix for this?

Please Sign in or register to post replies

Write your reply to:

Draft