Key/Value Editor package question

Umb V13.7.2

Anyone know how to programatically add keyvalue pairs to a KeyValue property editor from the Key/Value Editor package? I’m using the following but don’t even get the “Add” box available if I use it. I do if I comment the coursePage.Properties line below, but that defeats the object…

// modules is a Key/value editor from the package set up to handle 20 pairs.
coursePage.Properties["modules"]?.SetValue(await GetModules(courseRows, "Module"));


    Task<List<Vokseverk.KeyValuePropertyConverter.KeyAndValue>?> GetModules(List<CourseImportRow> courseRows, string what) {
        List<Vokseverk.KeyValuePropertyConverter.KeyAndValue> foundModules = new List<Vokseverk.KeyValuePropertyConverter.KeyAndValue>();

        foreach (CourseImportRow row in courseRows) {
            if (string.Equals(row.CourseInfoType?.Trim(), what, StringComparison.CurrentCultureIgnoreCase)) {
                string? rawModule = row?.CourseInfoText?.Trim();
                var match = Regex.Match(rawModule, @"\[(.*?)\]");
                string? moduleTitle = "";
                string? moduleDescription = "";
                
                if (match.Success) {
                    moduleTitle = match.Groups[1].Value;
                    moduleDescription = rawModule.Replace(match.Value, "").Trim() == "" ? "None" : rawModule.Replace(match.Value, "").Trim();
                }

                Vokseverk.KeyValuePropertyConverter.KeyAndValue newModule =
                    new Vokseverk.KeyValuePropertyConverter.KeyAndValue(
                        key: moduleTitle,
                        value: moduleDescription
                        );
                foundModules.Add(newModule);
            }
        }
        
        return Task.FromResult(foundModules);
    }

When run, the return foundModules has 7 correctly populated items, but nothing appears in the Node.

Where am I going wrong?

Thanks.

Hi Craig :waving_hand:

I’m the author of said PropertyEditor and I have no idea how to do that :rofl:

That said, it was heavily based on the built-in Repeatable TextStrings property editor, so maybe you can figure something out. It’s very likely that I didn’t implement something properly (e.g. the PropertyEditorValueConverter) to support that.

More than happy to accept a PR if you (or someone else) figure out what’s missing!

/Chriztian

1 Like

I had a quick look at the code and have two suggestions for you to try:

  1. Try creating the KeyAndValue objects using strings directly, e.g.:
Vokseverk.KeyValuePropertyConverter.KeyAndValue newModule =
    new Vokseverk.KeyValuePropertyConverter.KeyAndValue(moduleTitle, moduleDescription));
  1. Or set the properties afterwards, e.g.:
Vokseverk.KeyValuePropertyConverter.KeyAndValue newModule =
    new Vokseverk.KeyValuePropertyConverter.KeyAndValue();
newModule.Key = moduleTitle;
newModule.Value = moduleDescription;

Again: I’m not sure I fully understand the problem - could also be related to the whole Task approach (which needs the async / await dance) …

/Chriztian

I’m using your first method, and method returns what looks like a well populated dictionary item, but the issue may be with how I’m saving it to the content node which is:-

coursePage.Properties["modules"]?.SetValue(await GetModules(courseRows, "Module"));

Before doing the normal saveAndPublish().

I mean, is it legal to “await” the execution of a method while doing the SetValue(…)?

After looking at the Repeatable TextStrings docs, I also tried:-

Vokseverk.KeyValuePropertyConverter.KeyAndValue newModule =
                    new Vokseverk.KeyValuePropertyConverter.KeyAndValue(moduleTitle, moduleDescription);
                foundModules.Add(newModule);

string? foundModulesString = string.Join(Environment.NewLine, foundModules);
        
return Task.FromResult(foundModulesString);

But that didn’t work either :frowning:

I mean, is it legal to “await” the execution of a method while doing the SetValue(…)?

I don’t think so, no — I think the whole thing has to be labelled async if you use await somewhere inside… so it’s very likely that that’s the problem right there; the values are being created just fine, but the node has already been saved when the method returns with them, so to speak.

/Chriztian

I removed the await, and it didn’t make any difference. I might have to ditch this approach and do string manipulation instead. I get strings like “[this is a title]this is some text” which I was hoping to save as key/value, but I’ll just have to add the whole thing to a repeatable textstring and manipulate it in the view every time it’s displayed.

Thanks anyway.