koorlitz
02/01/2024, 10:28 AMkoorlitz
02/01/2024, 10:30 AMcsharp
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));
koorlitz
02/01/2024, 10:30 AMcsharp
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);
}
}
}
koorlitz
02/12/2024, 1:17 PMkoorlitz
02/21/2024, 6:22 AMkoorlitz
02/21/2024, 6:23 AMKevin Jump
02/21/2024, 9:00 AMkoorlitz
02/22/2024, 7:13 AMKevin Jump
02/22/2024, 9:19 AMcs
[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.
}
A hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by