Dynamic root query step
# help-with-umbraco
s
Has anyone in here worked with creating custom dynamic root node query steps (https://docs.umbraco.com/umbraco-cms/13.latest/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/multinode-treepicker#adding-a-custom-query-step)? I'm trying to do a query step, to find a global settings node in the root of my content tree. I think I've come to the conclusion to do it like below, but curious if others has done it in other ways.
Copy code
cs
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DynamicRoot.QuerySteps;
using Umbraco.Cms.Core.Extensions;
using Umbraco.Cms.Core.Services;

namespace Website.Core.DynamicRoot;

public class GlobalDataNodeDynamicRootQueryStep : IDynamicRootQueryStep
{
    private readonly IContentService _contentService;

    public GlobalDataNodeDynamicRootQueryStep(IContentService contentService)
    {
        _contentService = contentService;
    }

    protected virtual string SupportedDirectionAlias { get; set; } = "GlobalDataNode";

    public async Task<Attempt<ICollection<Guid>>> ExecuteAsync(ICollection<Guid> origins, DynamicRootQueryStep filter)
    {
        await Task.Run(() => { });

        if (filter.Alias != SupportedDirectionAlias)
        {
            return Attempt<ICollection<Guid>>.Fail();
        }

        if (origins.Count < 1)
        {
            return Attempt<ICollection<Guid>>.Succeed(Array.Empty<Guid>());
        }

        var globalDataNode = _contentService.GetRootContent().FirstOrDefault(x => x.ContentType.Alias == ContentModels.GlobalDataNode.ModelTypeAlias);

        if (globalDataNode == null)
        {
            return Attempt<ICollection<Guid>>.Succeed(Array.Empty<Guid>());
        }

        return Attempt<ICollection<Guid>>.Succeed(globalDataNode.Key.ToSingleItemCollection());
    }
}
l
Oh sweet, didn't know you could create your own query streps
d
Not on my worklaptop at the moment, so I can't show, but I did something similar. However, I didn't really bother with the checks that you're doing. I simply take the
IContentService
and fetch content at root and return the first result that has the content type alias that I'm looking for. I would've preferred to add it as a custom query root, but for some reason they won't let us extend those (at least not when I checked, which is already a while ago)
s
I was trying to do the origin too, even implemented one and added it using a composer. But when it wasn't showing up, I dug into the source code and saw that the list of origins is hardcoded 🙂
e
Yeah i wrote one to do exactly the same thing and it's pretty much what I ended up with
37 Views