Copied to clipboard

Flag this post as spam?

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


  • Carsten Nørregaard Panek 34 posts 124 karma points
    Dec 15, 2015 @ 12:16
    Carsten Nørregaard Panek
    0

    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.

  • John Churchley 272 posts 1258 karma points c-trib
    Dec 15, 2015 @ 13:05
    John Churchley
    0

    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?'

  • Carsten Nørregaard Panek 34 posts 124 karma points
    Dec 15, 2015 @ 13:20
    Carsten Nørregaard Panek
    0

    Hi John

    Thanks for your reply. Do you know of an example doing what I described in the question?

  • John Churchley 272 posts 1258 karma points c-trib
    Dec 15, 2015 @ 13:35
    John Churchley
    0

    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");
                            }
                        }
                    }
                }
            }
    
  • Carsten Nørregaard Panek 34 posts 124 karma points
    Dec 15, 2015 @ 13:46
    Carsten Nørregaard Panek
    1

    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.

  • Ian 178 posts 752 karma points
    Dec 15, 2015 @ 14:00
    Ian
    0

    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

  • John Churchley 272 posts 1258 karma points c-trib
    Dec 15, 2015 @ 14:06
    John Churchley
    101

    This works but the documentation suggest it doesn't always fire.

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
                {
                 ContentService.Created += ContentService_Created;
    
                }
    
      void ContentService_Created(IContentService sender, Umbraco.Core.Events.NewEventArgs<Umbraco.Core.Models.IContent> e)
                {
                    if (e.Entity.ContentType.Alias.ToString() == "DocumentType")
                    {
                        e.Entity.SetValue("PropertyName",PropertValue);
                    }
            }
    
  • Carsten Nørregaard Panek 34 posts 124 karma points
    Dec 16, 2015 @ 19:37
    Carsten Nørregaard Panek
    0

    Works like a charm, thank you very much!

  • Biagio Paruolo 1619 posts 1912 karma points c-trib
    Apr 26, 2018 @ 14:34
    Biagio Paruolo
    0

    +1

  • Paul Griffiths 370 posts 1021 karma points
    Jul 30, 2016 @ 10:12
    Paul Griffiths
    0

    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.

    public class DocTypeCreateEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Created += ContentService_Created;
            }
    
            void ContentService_Created(IContentService sender, Umbraco.Core.Events.NewEventArgs<Umbraco.Core.Models.IContent> e)
            {
                if (e.Entity.ContentType.Alias.ToString() == "homeFixture")
                {
                    e.Entity.SetValue("location", "Home");
                    e.Entity.SetValue("homeTeam", "Cefn Albion FC");
                }
                if (e.Entity.ContentType.Alias.ToString() == "awayFixture")
                {
                    e.Entity.SetValue("location", "Away");
                    e.Entity.SetValue("awayTeam", "Cefn Albion FC");
                }
    
            }
        }
    

    However, when the page is saved and published the default values are removed. Can anyone tell me where tim going wrong please?

    Thanks

    Paul

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Apr 26, 2018 @ 18:12
    Marc Goodson
    2

    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!

        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";
                        }
                    }
                }
            }
        }
    
  • Paul Griffiths 370 posts 1021 karma points
    May 03, 2018 @ 08:05
    Paul Griffiths
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft