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!
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.
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.
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.
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:
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!
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.
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.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.
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:
Find your property and set the value:
Your Nested Content property will now be pre-populated before saving the node.
is working on a reply...