Umbraco don't take my custom ISearchableTree
# help-with-umbraco
k
Hello, I created an ISearchableTree, I copied it from the documentation, but Umbraco doesn't take it into account/it does not work. Can anyone help me? (Umbraco v13) https://cdn.discordapp.com/attachments/1202561143126892564/1202561340082757662/image.png?ex=65cde785&is=65bb7285&hm=9938d6c2748695426b97fd4afa298ceb1662f7f134b67154299244f20d4a963f&
Code
Copy code
csharp
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.ContentEditing;

namespace Umbraco.Trees
{
    public class FavouriteeThingsTree : Umbraco.Cms.Core.Trees.ISearchableTree
    {
        public string TreeAlias => "favouriteeThingsAlias";

        public async Task<EntitySearchResults> SearchAsync(string query, int pageSize, long pageIndex, string searchFrom = null)
        {
            // your custom search implementation starts here
            Dictionary<int, string> favouriteThings = new Dictionary<int, string>
            {
                { 1, "Raindrops on Roses" },
                { 2, "Whiskers on Kittens" },
                { 3, "Skys full of Stars" },
                { 4, "Warm Woolen Mittens" },
                { 5, "Cream coloured Unicorns" },
                { 6, "Schnitzel with Noodles" }
            };

            var searchResults = new List<SearchResultEntity>();
            
            var matchingItems = favouriteThings.Where(f => f.Value.StartsWith(query, true, System.Globalization.CultureInfo.CurrentCulture));
Copy code
csharp
            foreach (var matchingItem in matchingItems)
            {
                // Making up the Id/Udi for this example! - these would normally be different for each search result.
                searchResults.Add(new SearchResultEntity()
                {
                    Id = 12345,
                    Alias = "favouriteThingItem",
                    Icon = "icon-favorite",
                    Key = new Guid("325746a0-ec1e-44e8-8f7b-6e7c4aab36d1"),
                    Name = matchingItem.Value,
                    ParentId = -1,
                    Path = "-1,12345",
                    Score = 1.0F,
                    Trashed = false,
                    Udi = Udi.Create("document", new Guid("325746a0-ec1e-44e8-8f7b-6e7c4aab36d1"))
                });
            }
            // Set number of search results found
            var totalFound = matchingItems.Count();
            // Return your results
            return new EntitySearchResults(searchResults, totalFound);
        }
    }
}
AAny?
please
And i've tried to register the ISearchableTree into the IComposer, without effect
k
Hi, Just had a look and i don't know when it changed in the code, but i think your ISearchableTree implimentation now needs to be on a TreeController. I had a searchable tree that use to work, but now like yours it doesn't look like its getting hit, I moved the implimentation to the tree controller (so added it to an existing tree controller move the methods). and now it gets hit on search 🤷‍♀️
k
Thanks you! Can you send me an exemple of the code ?
k
Hi, So if you arealy have a tree controller, you can add those above functions to it, and add ISerchableTree to the definition to say it is a searchable tree. if you don't have a tree controller, you need to make but you can make one that then isn't rendered as a tree !! i haven't tested this but i think this should work....
Copy code
cs
[Tree(Constants.Applications.Settings, "myTree", TreeUse = TreeUse.None)]
public class MySampleTree : TreeController, ISearchableTree
{
    public MySampleTree(
        ILocalizedTextService localizedTextService,
        UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection,
        IEventAggregator eventAggregator)
        : base(localizedTextService, umbracoApiControllerTypeCollection, eventAggregator)
    { }

    protected override ActionResult<MenuItemCollection> GetMenuForNode(string id, [ModelBinder(typeof(HttpQueryStringModelBinder))] FormCollection queryStrings)
        => null;

    protected override ActionResult<TreeNodeCollection> GetTreeNodes(string id, [ModelBinder(typeof(HttpQueryStringModelBinder))] FormCollection queryStrings)
        => null;

    /// searchable tree code here. 
}