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

Forums / Developing with Sitefinity / Handling an adding new item event in dynamic module

Handling an adding new item event in dynamic module

3 posts, 0 answered
  1. Sergey
    Sergey avatar
    1 posts
    Registered:
    01 Oct 2012
    24 Aug 2012
    Link to this post
    I've created new dynamic module with Module Builder (for example Products). I would like to do some actions when I add new item to content Products. Is it possible to handle that event?

    I will be very grateful to someone who will help with suggestions.
  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    30 Nov 2016
    29 Aug 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

    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


    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. Nidhi
    Nidhi avatar
    40 posts
    Registered:
    20 Jul 2010
    30 Aug 2012
    Link to this post
    Hi,
    I created a decorator for a dynamic module using the code you ve provided and also the kb.
    But I get this error in the global.asax file
    CS0308: The non-generic method 'Telerik.Microsoft.Practices.Unity.IUnityContainer.RegisterType(System.Type, System.Type, string, Telerik.Microsoft.Practices.Unity.LifetimeManager, params Telerik.Microsoft.Practices.Unity.InjectionMember[])' cannot be used with type arguments

    This is my complete global.asax file:
    <%@ Application Language="C#" %>
     
    <script runat="server">
     
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(Bootstrapper_Initialized);
        }
         
        void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
        {
     
            if (e.CommandName == "Bootstrapped")
            {
                Telerik.Sitefinity.Abstractions.ObjectFactory.Container.RegisterType<Telerik.Sitefinity.Lifecycle.ILifecycleDecorator, CustomDecorator.DynamicModulesDecorator>
                    (typeof(Telerik.Sitefinity.DynamicModules.DynamicModuleManager).FullName,
                        new Telerik.Microsoft.Practices.Unity.InjectionConstructor(
                            new Telerik.Microsoft.Practices.Unity.InjectionParameter<Telerik.Sitefinity.Lifecycle.ILifecycleManager>(null),
                            new Telerik.Microsoft.Practices.Unity.InjectionParameter<Telerik.Sitefinity.Lifecycle.LifecycleItemCopyDelegate>(null),
                            new Telerik.Microsoft.Practices.Unity.InjectionParameter<Type[]>(null)
                            )
                    );
            }
        }
         
        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
     
        }
             
        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
     
        }
     
        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
     
        }
     
        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends.
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer
            // or SQLServer, the event is not raised.
     
        }
            
    </script>


    UPDATE:

    For anyone else who is also getting the same error, you need to include the below in your Global.asax file:
    <%@ Import Namespace="Telerik.Microsoft.Practices.Unity" %>

3 posts, 0 answered