Copied to clipboard

Flag this post as spam?

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


  • Manish 373 posts 932 karma points
    Mar 03, 2017 @ 10:54
    Manish
    0

    Start up handler - ContentService_Published calling in loop

    Hi

    My code is like that but when i published or create any node it is calling ContentService_Published infinite times. Can someone please suggest what i am doing wrong here.

    public class ConfigurePublishedContentModelFactory : ApplicationEventHandler
    {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var types = PluginManager.Current.ResolveTypes<PublishedContentModel>();
            var factory = new PublishedContentModelFactory(types);
            PublishedContentModelFactoryResolver.Current.SetFactory(factory);
    
        }
    
        protected override void  ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
           Umbraco.Core.Services.ContentService.Published += ContentService_Published;
        }
    
    
        void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
        {
            var contentService = ApplicationContext.Current.Services.ContentService;
            var content = contentService.GetById(e.PublishedEntities.FirstOrDefault().Id);
            content.SetValue("bannerTitle", "This text w");
            contentService.Save(content);
            contentService.Publish(content);
        }
    
    }
    

    Manish

  • Manish 373 posts 932 karma points
    Mar 03, 2017 @ 10:55
    Manish
    0

    I just wanted to call only a new node is created or existing is published.

  • Micha Somers 134 posts 597 karma points
    Mar 03, 2017 @ 12:09
    Micha Somers
    0

    Calling Publish in the Published eventhandler, will again trigger a Published event and will call your ContentService_Published again.

    Without any conditional statements there's nothing that will prevent calling Publish again and again.

    So make sure that if you really need to call Publish in your Published event handler, the next call for this same item does not call Publish.

    A solution to prevent infinite calls, is by checking for the content of a particular property.

    In your example, if bannerTitle already contains the value "This text w" then do not call Publish.

  • Manish 373 posts 932 karma points
    Mar 03, 2017 @ 14:16
    Manish
    0

    Thanks for your reply

    But is there any other solution which allow only one call

    Manish

  • Micha Somers 134 posts 597 karma points
    Mar 03, 2017 @ 14:45
    Micha Somers
    100

    The possible events that you can use can be found here:

    Content Service Events

    Still, if you want to prevent calling an eventhandler more than once, you must make sure that you are not triggering it again in the eventhandler yourselves.

  • Manish 373 posts 932 karma points
    Mar 06, 2017 @ 05:45
    Manish
    0

    Many thanks

    Micha Somers

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies