Copied to clipboard

Flag this post as spam?

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


  • Paul Griffiths 370 posts 1021 karma points
    Jul 30, 2016 @ 14:49
    Paul Griffiths
    0

    Set default value of property using events

    Hi everyone,

    I really need some help setting a default value for a label property on my document types. After searching it has lead me to using events but im not having much luck.

    I have two document types in umbraco called homeFixture & awayFixture and i would like to set the default value of a label (that will never change) when a new page is created. I have the following code which sets the value when i choose to create a new page

    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", "Liverpool FC");
                }
                if (e.Entity.ContentType.Alias.ToString() == "awayFixture")
                {
                    e.Entity.SetValue("location", "Away");
                    e.Entity.SetValue("awayTeam", "Liverpool FC");
                }
    
            }
        }
    

    However, when i select save and publish the default values that i am seeing when i create the page are disappearing. Does anyone know how i can persist these values once saved and published?

     public class DocTypeCreateEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Published += ContentService_Created;
            }
    
            void ContentService_Created(IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
            {
                foreach (var node in e.PublishedEntities)
                {
                    if (node.ContentType.Alias.ToString() == "homeFixture")
                    {
                        node.SetValue("location", "Home");
                        node.SetValue("homeTeam", "Liverpool FC");
                    }
                    if (node.ContentType.Alias.ToString() == "awayFixture")
                    {
                        node.SetValue("location", "Away");
                        node.SetValue("awayTeam", "Liverpool FC");
                    }
                }          
    
            }
        }
    

    I have tried using this code to but having the same issue. It will update whn i select publish but when i revisit the node it disappears.

    Been on this for ages - I am using umbraco v7.4

    Thanks

  • Paul Griffiths 370 posts 1021 karma points
    Jul 30, 2016 @ 15:47
    Paul Griffiths
    0

    Not sure if its correct but using the Content.Saving did the trick.

    public class DocTypeCreateEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Saving += ContentService_Saving; 
            }
    
            void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)
            {
                foreach (var node in e.SavedEntities)
                {
                    if (node.ContentType.Alias.ToString() == "homeFixture")
                    {
                        node.SetValue("location", "Home");
                        node.SetValue("homeTeam", "Cefn Albion FC");
    
                    }
                    if (node.ContentType.Alias.ToString() == "awayFixture")
                    {
                        node.SetValue("location", "Away");
                        node.SetValue("awayTeam", "Cefn Albion FC");
                    }
                }
    
            }
        }
    

    Please let me know if there is a better way

    Paul

  • Ian 178 posts 752 karma points
    Jul 30, 2016 @ 18:58
    Ian
    0

    I'm not suggesting you change anything if its working, just seeing if you are aware of a new feature i came across recently. Its so new i havent used is but it looked potentially interesting for this sort of thing.

    EditorModelEventManager.SendingContentModel

  • Paul Griffiths 370 posts 1021 karma points
    Jul 31, 2016 @ 13:15
    Paul Griffiths
    0

    Hi Ian,

    Thanks for the tip! maybe something i can take a look into. I have it working but i still have an issue that you may be able to advise on?

    so i have the following code that checks whether the teamType (DDL value) is set as either first or reserve and apends the label value accordingly.

    public class DocTypeCreateEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Saving += ContentService_Saving; 
            }
    
            void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)
            {
                foreach (var node in e.SavedEntities)
                {
                    //get the team page to a list
                    var teamPage = node.Ancestors().Where(x => x.ContentType.Alias == "teamPage").ToList();
    
                    if (node.ContentType.Alias == "homeFixture")
                    {
                        foreach (var page in teamPage)
                        {
                            var teamType = umbraco.library.GetPreValueAsString(int.Parse(page.GetValue("teamType").ToString()));
    
                            if (teamType == "first")
                            {
                                node.SetValue("location", "Home");
                                node.SetValue("homeTeam", "Cefn Albion");
                            }
                            else
                            {
                                node.SetValue("location", "Home");
                                node.SetValue("homeTeam", "Cefn Albion Reserves");
                            }
                        }
    
                    }
                    if (node.ContentType.Alias == "awayFixture")
                    {
                        foreach (var page in teamPage)
                        {
                            var teamType = umbraco.library.GetPreValueAsString(int.Parse(page.GetValue("teamType").ToString()));
    
                            if (teamType == "first")
                            {
                                node.SetValue("location", "Away");
                                node.SetValue("awayTeam", "Cefn Albion");
                            }
                            else
                            {
                                node.SetValue("location", "Away");
                                node.SetValue("awayTeam", "Cefn Albion Reserves");
                            }
                        }
                    }
    
                }
    
            }
        }
    

    However, the following line of code returns 0 on the first save and publish and the values are not updated.

    var teamPage = node.Ancestors().Where(x => x.ContentType.Alias == "teamPage").ToList();
    

    once it has published and then i save and publish again the labels update as per the logic in my code. I suspect it has something to do with the face that the page doesnt exist yet?

    Any advice?

    Thanks Paul

  • Ian 178 posts 752 karma points
    Jul 31, 2016 @ 18:29
    Ian
    0

    Im not sure but that might be your problem exactly. You could see if there is anything else in the function arguments. I dont know if there would be a parentid property you could rely on i know at one point early on that is just -1 which wouldnt be any good. Path would also give you a parent to then use ancestor on if its populated yet. And then theres maybe looking at the request grabbing the id from the url when it looks like ...?create=true. If you look at the behaviour i think the id gets populated with the tree node clicked when creating which gives you some context.

    Someone else may know better.

Please Sign in or register to post replies

Write your reply to:

Draft