Copied to clipboard

Flag this post as spam?

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


  • Josh G 12 posts 53 karma points
    Sep 01, 2017 @ 17:33
    Josh G
    0

    ContentService.Published Events Preventing Publishing

    Im trying to create a couple of events to handle content when articles are published..

    So I've got the following

    namespace MySite.AppEvents
    {
        public class RegisterApplicationEvents : ApplicationEventHandler
        {
    
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                JsEngineSwitcherConfig.Configure(JsEngineSwitcher.Instance);
                BundleConfig.RegisterBundles(System.Web.Optimization.BundleTable.Bundles);
                ContentService.Saving += ContentService_Saving;
                ContentService.Published += ContentService_Published;
                ContentService.UnPublished += ContentService_UnPublished;
            }
    
            void ContentService_UnPublished(IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> e)
            {
                if (e.PublishedEntities.Any())
                {
                    foreach (var entity in e.PublishedEntities)
                    {
    
                        UmbracoDatabase db = ApplicationContext.Current.DatabaseContext.Database;
                        var delete = new Sql("DELETE FROM [dbo].[ContentRelationships] Where NodeId =" + entity.Id);
                        db.Execute(delete);
                    }
    
                }
            }
    
            void ContentService_Published(IPublishingStrategy sender, PublishEventArgs<Umbraco.Core.Models.IContent> e)
            {
                if (e.PublishedEntities.Any())
                {
                    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                    foreach (var node in e.PublishedEntities)
                    {
                        UmbracoDatabase db = ApplicationContext.Current.DatabaseContext.Database;
    
                            ////do some stuff here
    
                    }
                }
            }
    
            void ContentService_Saving(IContentService contentService, SaveEventArgs<IContent> eventArgs)
            {
    
                foreach (var node in eventArgs.SavedEntities)
                {
                    if (node.ContentType.Alias == "product")
                    {
    
                      // do some stuff here
    
                    }
                }
            }
    
        }
    
    }
    

    So this works and my custom function create the database rows as expected.

    But the new page doesn't publish.

    I can see it in the backend, but if i visit the URL I get a 404.

    Am I preventing the actual publish/save happening somehow?

Please Sign in or register to post replies

Write your reply to:

Draft