Dynamic root query step
s

skttl

10 months ago
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.
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());
    }
}
[SOLVED] How to set configuration on DataType in code in Umbraco 15 (compared to 13)
l

Luuk Peters (Proud Nerds)

11 months ago
The answer is probably simple, but I can't find a way to correctly update some code from Umbraco 13 into the 15 equivalent. I have the following code in Umbraco 13 (redacted for easier reading):
csharp
var configuration = new SliderConfiguration
{
    EnableRange = false,
    MinimumValue = new decimal(0),
    MaximumValue = new decimal(1),
}

var editorAlias = Umbraco.Cms.Core.Constants.PropertyEditors.Aliases.Slider;

//How to get the editor from the alias is not important
//serializer is an injected IConfigurationEditorJsonSerializer
var newType = new DataType(editor, serializer)
{
    DatabaseType = _databaseType ?? ValueStorageType.Ntext,
    CreateDate = DateTime.Now,
    CreatorId = -1,
    Name = _dataTypeName,
    Configuration = configuration
};
In this case, Configuration is an object?. And Umbraco has models (like the SliderConfiguration) for the configuration of the editor associated with the datatype. However, things change a little (but enough) in Umbraco 15: Configuration is now ConfigurationData and no longer a object?, but a Dictionary. Problem 1: I assume that the dictionary is a collection where the string is a property name and the object it's value. However, I'm not sure because I can't find any documentation on it. Is that's the case, what do I need to use as the keys? I mean, the SliderConfiguration class has some annotations, do I need to use these as key?
csharp
public class SliderConfiguration
{
    [ConfigurationField("enableRange")]
    public bool EnableRange { get; set; }

    [ConfigurationField("minVal")]
    public decimal MinimumValue { get; set; }

    [ConfigurationField("maxVal")]
    public decimal MaximumValue { get; set; }
}
Problem 2 is that I have a perfectly good strong typed model. I don't want to create a dictionary myself and trying to figure out what keys to use. I think there should be some converter/parser that could create the dictionary of my model. Any thoughts?