Copied to clipboard

Flag this post as spam?

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


  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Nov 22, 2013 @ 19:34
    Jesper Ordrup
    1

    Umbraco 7 events - how to catch the creating event

    Hi there,

    I'm trying to modify some document propeties when creating the document but I fail.  

     

    I've created the following:

    using System; using Umbraco.Core; using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Publishing; using Umbraco.Core.Services;

    namespace jesper

    {

        public class myFancyClass : ApplicationEventHandler

        {

            public myFancyClass()

            {

                ContentService.Creating += Creating;

            }

            private void Creating(IContentService sender,  NewEventArgs<IContent> args) {

     

                var node = args.Entity;

                try

                {

                    node.SetValue("myproperty", "fish is great");

                }

                catch { }

    } } }

     

    What happends is nothing :-)

    I cant debug umbraco7 as I havent managed to get it to run in Visual Studio yet.

    Any obvious misses?

    best
    Jesper

     

    ps. catching the Publishing event and modifying content using the same approach works fine.

     

  • Comment author was deleted

    Nov 22, 2013 @ 20:16

    Hmm think the creating event might be to soon, try moving to created

  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Nov 22, 2013 @ 23:22
    Jesper Ordrup
    0

    Hi Tim 

    I tried:

      ContentService.Created+=theSameCode;

     

    Same same .. nothing is saved. I havent even tried to call save after as I cant imagine but ?


  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Nov 23, 2013 @ 15:39
    Jesper Ordrup
    0

    Ive installed Umbraco 7 using VS + nuget and captured ContentService.Created, ContentService.Creating & ContentService.Publishing. It seems that the two create related Events are never thrown as none of my breakpoints are hit. In the publicing event my breakpoints are hit. 

    I dont know if its a bug or just changed behavior as in U 7 we dont create it before we save it I guess  Before - we actually named and created a node before editing and then saving.

    If thats the case - how do we add inital values to fields from events?

    best
    Jesper 

     

     

  • Comment author was deleted

    Nov 25, 2013 @ 13:39

    SO looks like a bug, thanks for putting it on the issue tracker http://issues.umbraco.org/issue/U4-3695

  • Shannon Deminick 1524 posts 5269 karma points MVP 2x
    Nov 26, 2013 @ 02:44
    Shannon Deminick
    0

    I've replied on the tracker: http://issues.umbraco.org/issue/U4-3695

    both the creating and created events will be obsoleted and should not be used, they are only there for legacy purposes and only fire when using the ContentService.CreateContent to instantiate a new IContent item which is not a mandatory way to create content so it is inconsistent at best (i.e. If a package I release does not use this method to create content and instead i just do: var mycontent = new Content(), the event will not fire)

    Use the Saving event instead, you can determine there if the IContent instance is new.

  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Nov 26, 2013 @ 07:57
    Jesper Ordrup
    0

    I see the problem. Thx.
    Related to that - does Umbraco 7 provide a frontend solution to this? A angularjs event that fires onload that we can hook into in a nonhack way?

     

  • Shannon Deminick 1524 posts 5269 karma points MVP 2x
    Nov 26, 2013 @ 08:01
    Shannon Deminick
    0

    Currently no it doesn't. we will provide a c# API that allows you to modify the model created by the controllers before they are being sent to the view but that will be in 7.1. That will allow you to do all sorts of crazy stuff like even adding custom tabs/properties that don't actually exist on the document type (whilst still being validated on the server side post ). With that we can create an angular event - which you could modify model values but you won't be able to add/remove entire properties that way since it won't validate on the server side.

  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Nov 26, 2013 @ 08:04
    Jesper Ordrup
    0

    Good stuff. Thx

  • Ryios 122 posts 263 karma points
    Jan 07, 2015 @ 07:38
    Ryios
    0

    Any updates on the API you will provide in 7.1, we are at 7.2.1 and I'm working with these now and wondering if it's in 7.2.1

  • Shannon Deminick 1524 posts 5269 karma points MVP 2x
    Jan 08, 2015 @ 00:05
    Shannon Deminick
    0

    Hi, you can track the two task items required for this here:

    http://issues.umbraco.org/issue/U4-2670 http://issues.umbraco.org/issue/U4-2671

  • Oscar Svedberg 19 posts 82 karma points
    Jan 28, 2015 @ 00:15
    Oscar Svedberg
    2

    I'm running the below code which works just fine. Maybe it'll help you!

    public class SetDefaultValues : IApplicationEventHandler
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(SetDefaultValues));
    
        void IApplicationEventHandler.OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        void IApplicationEventHandler.OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        void IApplicationEventHandler.OnApplicationStarting(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.HasProperty("layout"))
            {
                return;
            }
    
            var layout = "layout-1";
    
            if (e.Entity.ContentType.Alias == "DocumentTypeWithLayout-2")
            {
                layout = "layout-2";
            }
    
            e.Entity.SetValue("layout", layout);
        }
    }
    
  • Jesper Ordrup 1019 posts 1528 karma points MVP
    Jan 29, 2015 @ 10:52
    Jesper Ordrup
    0

    Wow - that was an old topic. Thanks for update. Looks great. I'll try it out.

    best
    Jesper 

Please Sign in or register to post replies

Write your reply to:

Draft