This documentation suggest this should work however it only fires when you save the document for the first time! Maybe someone else in the community knows a solution.
public class ApplicationEventHandler : IApplicationEventHandler
{
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Saving += DocumentTypeName;
}
private void DocumentTypeName(IContentService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
{
var Pages = e.SavedEntities.Where(x => x.ContentType.Alias == "DocumentTypeAlias");
if (Pages.Count() > 0)
{
foreach (var page in Pages)
{
if (!page.HasIdentity)
{
page.SetValue("propertyname", "prepopulatedvalueyouwant");
}
}
}
}
}
But I would like the values to be in the fields as soon as the new document is created, with this the values first get filled when the document is saved.
Hi there, I know you can do that with the archetype package as it creates its own wrapper round inbuilt fields to extend functionality, but know of no other way out of the box
I am trying to set the default value of a label on a document type call homeFixture and awayFixture. Using the following code when i create a new page using the home & away doctypes it sets the values as expected.
Here you can set the default values for the properties as the page loads for the editor to edit, so they can see the default values in place, before they save the content.
It's more obvious to the editor then what the default values will be!
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Web.Editors;
using Umbraco.Web.Models.ContentEditing;
namespace My.Namespace
{
public class MyEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel;
}
private void EditorModelEventManager_SendingContentModel(System.Web.Http.Filters.HttpActionExecutedContext sender, EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
{
if (e.Model.ContentTypeAlias == "homeFixture")
{
var locationProperty = e.Model.Properties.FirstOrDefault(f => f.Alias == "location");
var homeTeamProperty = e.Model.Properties.FirstOrDefault(f => f.Alias == "homeTeam");
if (locationProperty.Value == null || String.IsNullOrEmpty(locationProperty.Value.ToString()))
{
locationProperty.Value = "Home";
}
if (homeTeamProperty.Value == null || String.IsNullOrEmpty(homeTeamProperty.Value.ToString()))
{
homeTeamProperty.Value = "Cefn Albion FC";
}
}
if (e.Model.ContentTypeAlias == "awayFixture")
{
var locationProperty = e.Model.Properties.FirstOrDefault(f => f.Alias == "location");
var awayTeamProperty = e.Model.Properties.FirstOrDefault(f => f.Alias == "awayTeam");
if (locationProperty.Value == null || String.IsNullOrEmpty(locationProperty.Value.ToString()))
{
locationProperty.Value = "Away";
}
if (awayTeamProperty.Value == null || String.IsNullOrEmpty(awayTeamProperty.Value.ToString()))
{
awayTeamProperty.Value = "Cefn Albion FC";
}
}
}
}
}
Default value on property in document type
How do I make default values on a property in certan document types?
I would like that some fields on a certan document type is filled with default values, when a new document is created.
Latch on to the ContentService.Saving event more details can be seen here:
https://our.umbraco.org/documentation/reference/events/contentservice-events
Check out the subsection titled 'What happened to Creating and Created events?'
Hi John
Thanks for your reply. Do you know of an example doing what I described in the question?
This documentation suggest this should work however it only fires when you save the document for the first time! Maybe someone else in the community knows a solution.
Very cool, works fine.
But I would like the values to be in the fields as soon as the new document is created, with this the values first get filled when the document is saved.
Hi there, I know you can do that with the archetype package as it creates its own wrapper round inbuilt fields to extend functionality, but know of no other way out of the box
This works but the documentation suggest it doesn't always fire.
Works like a charm, thank you very much!
+1
Hi all,
I am trying to set the default value of a label on a document type call homeFixture and awayFixture. Using the following code when i create a new page using the home & away doctypes it sets the values as expected.
However, when the page is saved and published the default values are removed. Can anyone tell me where tim going wrong please?
Thanks
Paul
Hi Paul
I find the best way to set a default value, is to use the EditorModelEventManager.SendingContentModel event
https://our.umbraco.org/documentation/Reference/Events/EditorModel-Events
Here you can set the default values for the properties as the page loads for the editor to edit, so they can see the default values in place, before they save the content.
It's more obvious to the editor then what the default values will be!
Hi Marc,
Sorry for the late reply, competently forgot! I was watching your Anti Patterns talk for CG17 and it prompted me that i haven't replied :).
Thank you for taking the time to provide that detailed answer with code examples :).
I will take a look at this :).
Thanks
Paul
is working on a reply...