Default property editor value not published when creating content using Content Service
I have a checkbox property editor on a document type that is checked by default.
I'm using the ContentService to create and publish the node.
var node = _contentService.Create(nodeName, parent.Id, contentTypeAlias);
_contentService.SaveAndPublish(node);
It seems to work as expected, if I go to the node in the content tree everything looks exactly as I would expect. The checkboxes are checked there is no green + icon indicating a saved draft.
But my razor view for my sitemap is showing those values as false.
And if I rollback, there is only one version and the diff shows that the current true values would be removed if I rollback to it
If I click "Save and Publish" the values all update to the expected state.
Since these folders are being generated I'd like to make sure that they are hidden by default instead of having to navigate to them and click save and publish. I could explicitly set the values after I create the content and publish it but is there something I can/have to do to make sure the default values are set when content is created?
I can't remember exactly and not in a place where I can look at past projects, but I remember something funky about how the cache gets updated with the SaveAndPublish event.
I don't think it's cache related it seems to not be saving any value in the database. The angular controller has some logic to apply the default if there is no value on the model. So the frontend is showing the default value even though the db has nothing stored.
The 'default' value for the Checkbox property editor, is only a 'starting position' for that editor, when it is opened in the backoffice on a new document type.
So if an editor creates a new page in the backoffice, and it has a checkbox with a 'default value' set to be true, then, it will appear to the editor as being already checked.
There is an event that fires in the backoffice where these kind of 'starting positions' for editors can be set:
But if you are creating content programatically, this is outside the UI of the backoffice and these events do not fire, and so you need to take responsibility for setting any default values yourself.
The grey area then is if you are writing some code to create a content item, and it has a checkbox, and you want this default state to always reflect what has been selected in the backoffice as the default value for the checkbox, and you have no control over whether this value changes?
It all depends on the context of what you are trying to do! - but if I wanted the nodes that were being created automatically to always have those checkboxes checked, I would put that logic in the creation of them.
var node = _contentService.Create(nodeName, parent.Id, contentTypeAlias);
//can't remember if this is 'true' or '1' that you need to set!
node.SetValue("hideFromSitemap",true)
node.SetValue("hideFromNavigation",true)
_contentService.SaveAndPublish(node);
instead of trying to use the configuration values of the property in the CMS - I can see niche circumstance where you might 'have to', (and it would be your code below) , but it would be an unnecessary overhead if pretty much the values were ALWAYS true here and never changed!
Anyway this is why you are seeing what you are seeing, you are creating the object in code, no value is set for hideFromSiteMap or hideFromNavigation - you open the node in Umbraco, and because no value is set in the database, the 'default' values appears for the checkboxes, saving and publishing then updates the database with these 'default' values, but if you don't save and publish, they are not in the database, and not in the published cache - hence you can't use them in razor!
I think the problem is calling them 'default' values in the configuration, as they don't behave how you would think 'default' values to behave, but they are a handy configuration for the starting position of a property editor...
Default property editor value not published when creating content using Content Service
I have a checkbox property editor on a document type that is checked by default.
I'm using the ContentService to create and publish the node.
It seems to work as expected, if I go to the node in the content tree everything looks exactly as I would expect. The checkboxes are checked there is no green + icon indicating a saved draft.
But my razor view for my sitemap is showing those values as false.
And if I rollback, there is only one version and the diff shows that the current true values would be removed if I rollback to it
If I click "Save and Publish" the values all update to the expected state.
Since these folders are being generated I'd like to make sure that they are hidden by default instead of having to navigate to them and click save and publish. I could explicitly set the values after I create the content and publish it but is there something I can/have to do to make sure the default values are set when content is created?
For now I'm retrieving the configuration using the DataTypeService and setting the default manually
I can't remember exactly and not in a place where I can look at past projects, but I remember something funky about how the cache gets updated with the SaveAndPublish event.
This is what I was thinking of, maybe try Publish() instead? https://stackoverflow.com/questions/21681631/umbraco-contentservice-not-updating-checboxlist-in-document-cache
I don't think it's cache related it seems to not be saving any value in the database. The angular controller has some logic to apply the default if there is no value on the model. So the frontend is showing the default value even though the db has nothing stored.
I think it's related to this https://github.com/umbraco/Umbraco-CMS/issues/6252#issuecomment-526917401
Ah interesting.
Hi Kevin
The 'default' value for the Checkbox property editor, is only a 'starting position' for that editor, when it is opened in the backoffice on a new document type.
So if an editor creates a new page in the backoffice, and it has a checkbox with a 'default value' set to be true, then, it will appear to the editor as being already checked.
There is an event that fires in the backoffice where these kind of 'starting positions' for editors can be set:
https://our.umbraco.com/documentation/reference/events/EditorModel-Events/
But if you are creating content programatically, this is outside the UI of the backoffice and these events do not fire, and so you need to take responsibility for setting any default values yourself.
The grey area then is if you are writing some code to create a content item, and it has a checkbox, and you want this default state to always reflect what has been selected in the backoffice as the default value for the checkbox, and you have no control over whether this value changes?
It all depends on the context of what you are trying to do! - but if I wanted the nodes that were being created automatically to always have those checkboxes checked, I would put that logic in the creation of them.
instead of trying to use the configuration values of the property in the CMS - I can see niche circumstance where you might 'have to', (and it would be your code below) , but it would be an unnecessary overhead if pretty much the values were ALWAYS true here and never changed!
Anyway this is why you are seeing what you are seeing, you are creating the object in code, no value is set for hideFromSiteMap or hideFromNavigation - you open the node in Umbraco, and because no value is set in the database, the 'default' values appears for the checkboxes, saving and publishing then updates the database with these 'default' values, but if you don't save and publish, they are not in the database, and not in the published cache - hence you can't use them in razor!
I think the problem is calling them 'default' values in the configuration, as they don't behave how you would think 'default' values to behave, but they are a handy configuration for the starting position of a property editor...
regards
Marc
is working on a reply...