Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gordon Saxby 1444 posts 1855 karma points
    Jun 18, 2019 @ 11:52
    Gordon Saxby
    0

    Default values for Nested Content fields

    I am using Nested Content and I want to set 2 of the fields ("Entered By" and "Entered Date") to a default value if not entered by the user.

    I have been trying to figure out the code to go in "ContentService.Saving" but am not doing very well!!

    I initially thought about converting he IContent to an IPublishedContent but that doesn't seem possible / straightforward. I then started to look at getting the raw content (JSON, I assume), updating that and then putting it back. That might be the solution but I'm struggling to figure out the code / process.

    Any suggestions / pointers / template code would be most welcome!

  • Ronald Barendse 39 posts 217 karma points hq c-trib
    Oct 16, 2019 @ 08:23
    Ronald Barendse
    0

    Setting default values can be done in the EditorModelEventManager.SendingContentModel event (see documentation), but that works on a content level, not for the dynamically added Nested Content elements...

    The NC data is stored as JSON, so you could use the ContentService.Saving event to parse the data and set the default values within it... Not a nice way (especially if you see what's required to parse the data) and you can't use client-side validation, e.g. required properties (as it's validated before saving).

    So for now, there doesn't seem to be a good solution. I would opt for adding default values to all property editors, so it's just part of configuring the data type.

  • Neil Chapman 42 posts 169 karma points
    Dec 02, 2022 @ 17:11
    Neil Chapman
    0

    Hey Ronald,

    I have spent the last few hours trying to do exactly what you say isn't possible, pre-populate a Nested Content property from the EditorModelEventManager_SendingContentModel event.

    I am populating in the same way as I have previously done with IContent but no matter what I try (even taking the generated JSON output when saving through the backoffice), it just won't populate.

    testValues.Add(new Dictionary<string, object>()
    {
        { "key", Guid.NewGuid().ToString() },
        { "name", "Test string value" },
        { "ncContentTypeAlias", "nCTest" },
        { "testString", "Test string value" }
    });
    property = JsonConvert.SerializeObject(testValues);
    

    Is there a reason why this isn't possible/an alternative route? I am thinking it's gonna have to be a custom property editor.

    When creating a new doc type, I need to pre-populate a nested content property with data from elsewhere in the system, so doing this on a NC default value/saving event isn't a solution unfortunately.

    Any thoughts/ideas you have would be greatly appreciated.

  • Neil Chapman 42 posts 169 karma points
    Dec 05, 2022 @ 10:31
    Neil Chapman
    0

    Hi Ronald,

    I have actually managed to get this working for Nested Content.

    The property when sent to the backoffice is a ContentPropertyBasic model and the value is expecting an object, not json, so simply passing the Nested Content object without serializing works.

    For anyone coming to this wanting to do the same thing, build your Nested Content object:

    nestedContent.Add(new Dictionary<string, object>()
    {
        { "key", Guid.NewGuid().ToString() },
        { "ncContentTypeAlias", "ncAlias" },
        { "name", "NC item name" },
        { "ncPropertyAlias", "Property value" }
    });
    

    Find your property and set the value:

    var property = content.Tabs.SelectMany(tab => tab.Properties).FirstOrDefault(property => property.Alias.InvariantEquals(propertyAlias));
    property.Value = nestedContent;
    

    Your Nested Content property will now be pre-populated before saving the node.

Please Sign in or register to post replies

Write your reply to:

Draft