Copied to clipboard

Flag this post as spam?

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


  • Keith Boynton 19 posts 61 karma points
    Aug 09, 2020 @ 15:10
    Keith Boynton
    0

    Scheduled publish with descendants in v8

    Hey folks..

    I'm hoping this is me just missing something but it seems to me that it's impossible to do a scheduled publish with descendants and there appears to be no workaround.

    I can't publish the descendants first as the parent needs to be published, if I schedule the parent then the descendants aren't published and there is no option I can see on the scheduling screen to include descendants.

    Is the only way with some custom code hooked into the publish event or am I being daft and missing something?

  • David Armitage 505 posts 2073 karma points
    Aug 13, 2020 @ 05:12
    David Armitage
    0

    Hi Keith,

    If this is not available out of the box what I would do is...

    1. Schedule the publish of the parent

    2. Then create a PublishedEvent to track this parent. https://our.umbraco.com/documentation/reference/events/

    3. Then trigger saving and publishing all its children from within your PublishedEvent

    This should work very well. I have done a similar thing many times.

    Regards

    David

  • Keith Boynton 19 posts 61 karma points
    Sep 08, 2020 @ 16:37
    Keith Boynton
    0

    Hi David,

    Just returned to this after working on something else. Thank you very much for the response, I'm going to take a look at this in the morning.

  • Keith Boynton 19 posts 61 karma points
    Sep 09, 2020 @ 11:46
    Keith Boynton
    1

    Hi again David,

    Thank you so much for the steer here, made total sense.

    Here's how I implemented it for anyone looking for the same information:

    In my custom publishing component where I suscribe to events:

    I make sure I've got an IContentService I can use:

    public CustomPublishingComponent(ILogger logger, IUmbracoContextFactory contextFactory, IContentService contentService)
            {
                _logger = logger;
                _contextFactory = contextFactory;
                _contentService = contentService;
            }
    

    I then subscribe to the published event:

    // Subscribe to the content service publishing and published events
                ContentService.Publishing += ContentService_Publishing;
                ContentService.Published += ContentService_Published;
    

    It's the published one that matters here, using the publishing one won't work on the first attempt as the parent won't be published yet.

    And then in the event handler I just do this:

         private void ContentService_Published(IContentService sender, ContentPublishedEventArgs e)
            {
                // Handle any podcast episodes that have been published
                var podcastEpisodeEntitiesPublished = e.PublishedEntities.Where(x => x.ContentType.Alias.Equals("podcastEpisode")).ToList();
                HandlePublishedPodcastEpisodes(podcastEpisodeEntitiesPublished, e);
            }
    
         private void HandlePublishedPodcastEpisodes(List<IContent> publishedPodcastEpisodeEntities, ContentPublishedEventArgs e)
            {
                foreach (var podcastEpisodeEntity in publishedPodcastEpisodeEntities)
                {
                        // Publish all the track nodes of the episode
                        var trackCount = _contentService.CountChildren(podcastEpisodeEntity.Id, "podcastTrack");
                        if (trackCount > 0)
                        {
                            var trackNodes = _contentService.GetPagedChildren(podcastEpisodeEntity.Id, 0, trackCount, out var total);
                            foreach (var trackNode in trackNodes)
                            {
                                var result = _contentService.SaveAndPublish(trackNode, raiseEvents: false);
                                _logger.Debug<CustomPublishingComponent>("published podcast track {Result}", result);
                            }
                        }
                }
            }
    

    Thank you again David :)

  • David Armitage 505 posts 2073 karma points
    Sep 09, 2020 @ 11:55
    David Armitage
    0

    No problems at all. Glad I could help.

  • Remko 118 posts 283 karma points
    Sep 12, 2023 @ 08:58
    Remko
    0

    This is a bit old topic, but hopefully you guys are still active here...

    Still not figured out (now already in Umbraco 12) how to make this work.

    When I schedule a Save and publish it seems the publishing and published events both arent hit, so how did you manage to get this working then?

Please Sign in or register to post replies

Write your reply to:

Draft