1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Subscribe to Create and/or Update events dynamic modules

Subscribe to Create and/or Update events dynamic modules

6 posts, 0 answered
  1. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    06 Nov 2012
    Link to this post
    Hi,

    I'd like to know if it is possible to subscribe to the Create and Update events of a dynamic module item.

    I'd like to store a shortened url within a dynamic content and to me it seems that the best place to generate such url, is during the creation or update of an item.

    If someone knows how this could be done, or knows a better way, please share it?

    Thanks!
    Daniel
  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    17 Aug 2017
    09 Nov 2012
    Link to this post
    Hi,

    Yes this is possible by subscribing to the lifecycle events of the content modules. The approach is to create a custom decorator as described in this KB article. The approach is to have a separate decorator for each module you want to handle lifecycle events like publish, modify (delete is a separate topic). The decorator attaches to an event, for example publish.

    When we talk about a custom decorator that will handle dynamic content (module builder) you have to change the registration of the decorator in Global.asax. This is necessary because in Dynamic modules the items you create are not inheriting from content and while all other modules (news, blogs, events), register a decorator that will handle content
    new InjectionParameter<Action<Content, Content>>(null),
    Dynamic modules register the decorator like this:
    protected void Application_Start(object sender, EventArgs e)
           {
               Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized);
           }
       
           void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
           {
       
               if (e.CommandName == "Bootstrapped")
               {
                   ObjectFactory.Container.RegisterType<ILifecycleDecorator, CustomDecorator.NewsDecorator>
                       (typeof(DynamicModuleManager).FullName,
                           new InjectionConstructor(
                               new InjectionParameter<ILifecycleManager>(null),
                               new InjectionParameter<LifecycleItemCopyDelegate>(null),
                               new InjectionParameter<Type[]>(null)
                               )
                       );
               }
           }
    For the module builder each item you create regardless of the naming of your module it is considered DynamicContent so in the decorator register the item you will be working with like this
    DynamicContent newsItem = masterItem as DynamicContent;
                   
                // add logic that will be executed when item is publsihed
       
                return base.ExecuteOnPublish(masterItem, liveItem, culture, publicationDate);
    I realize you will need to work with a lot of different modules with the module builder and creating new decorator for each separate module will not be possible so specify the type of module you will be executing something on publish like this.
    DynamicContent dynamicContent = item as DynamicContent;
                string dynamicContentType = dynamicContent.GetType().FullName;
    if (dynamicContentType.Equals(TesetingModules.TestModule.FullName))
     {
    // execute logic for one module called TestingModule
    }
    .. add another module logic below

    Execute your update of the field on publish as the functionality of the decorator.
    In development now are events for the Module builder that will alow you to execute additional actions on all events of module builder items to  address more cleaner way of handling this task.

    Kind regards,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    09 Nov 2012 in reply to Stanislav Velikov
    Link to this post
    Hi Stanislav,

    I will figure it out with this information.
    Thanks for the explanation and help!

    Regards,
    Daniel
  4. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    12 Nov 2012
    Link to this post
    From Slavo: http://www.sitefinity.com/documentation/documentationarticles/developers-guide/deep-dive/sitefinity-event-system/list-of-events/dynamic-modules-events
  5. Rico
    Rico avatar
    88 posts
    Registered:
    30 Aug 2012
    21 Mar 2013 in reply to Steve
    Link to this post
    Hi there,

    I've followed these posts in the past and had them working as expected in v5.2; however I seem to have encountered an issue with 5.4. (which I don't believe was there in the previous version) I have a dynamic module created, and I go to update the content (update the description text). All of the events are fired now (whereas I swear in the past they weren't). the ContentCreatedEventHandler, ContentDeletedEventHandler and ContentUpdatedEventHandler (multiple times to boot!). This doesn't seem right to me (if I update content, then I expect only the ContentUpdatedEventHandler to fire -- not the others). ... any ideas? Has anybody else encountered this?

    How I have it wired up in global.asax:
    if (e.CommandName == "Bootstrapped")
    {
    EventHub.Subscribe<IDynamicContentCreatedEvent>(evt => this.ContentCreatedEventHandler(evt));
    EventHub.Subscribe<IDynamicContentDeletedEvent>(evt => this.ContentDeletedEventHandler(evt));
    EventHub.Subscribe<IDynamicContentUpdatedEvent>(evt => this.ContentUpdatedEventHandler(evt));
    EventHub.Subscribe<IMediaContentDownloadedEvent>(evt => this.MediaContentDownloadedEven(evt));
    }

    It seems as though for dynamic content items, when you update an existing item, sitefinity actually deletes that item, and then re-creates it?? is that right?

    Thanks,

  6. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    17 Aug 2017
    26 Mar 2013
    Link to this post
    Hi,

    Try with this snippet to attach to an event.

                if (e.CommandName == "Bootstrapped")
                {
                    EventHub.Subscribe<IDynamicContentCreatedEvent>(new SitefinityEventHandler<IDynamicContentCreatedEvent>(OnLogin));
                }
     
    //...
     
            void OnLogin(IDynamicContentCreatedEventloginEvent)
            {
    }

    Wehn you update an item you checkout this item in Temp state, edit the Temp item and when performing publiosh the Temp is check in to the Master item and the Temp is deleted. More information on this lifecycle operation here.

    All the best,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
6 posts, 0 answered