Copied to clipboard

Flag this post as spam?

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


  • Ryan Noble 17 posts 85 karma points
    Mar 17, 2017 @ 14:23
    Ryan Noble
    0

    Access Content Service or Typed Content in Publish event

    So I've run into a problem where I'm trying to copy content to other nodes of a multi region/language site.

    I've hooked into the ContentService.Publishing event.

    So the goal is:

    When on the main node of the site, publishing from a news module, if the publish is under the main node, take that content node thats to be published and clone to the other root nodes.

    So Global -> News module -> Article 1

    Article one was just published, I need to intercept that publish and clone it to the other sections like Canada -> News Module -(Cloned) Article 1

    I cant find a way to access ContentService getby functions or Umbraco.Typed content at all in this context.

    I looked at this: https://glcheetham.name/2016/05/27/implementing-ioc-in-umbraco-unit-testing-like-a-boss/

    However this is an angular site so MVC context doesnt apply.

    Any ideas?

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 17, 2017 @ 15:26
    Dan Diplo
    1

    You probably want to use the content service Published event, as that executes once the content has finished being published.

    You can access all the Umbraco services via the ApplicationContext singleton in Umbraco.Core eg.

    ApplicationContext.Current.Services.ContentService;
    

    Example:

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        ContentService.Published += ContentService_Published;
    }
    
    private void ContentService_Published(IPublishingStrategy service, PublishEventArgs<IContent> e)
    {
        var contentService = ApplicationContext.Current.Services.ContentService;
        int parentPageToCopyToId = 12345;
    
        foreach (var doc in e.PublishedEntities)
        {
            contentService.Copy(doc, parentPageToCopyToId, true);
        }
    }
    
  • Ryan Noble 17 posts 85 karma points
    Mar 17, 2017 @ 15:34
    Ryan Noble
    0

    I was thinking it would be available then, but the issue is I dont want to run this on every publish, only on the first publish.

    In publishing I could look at the has Identity field but after publish it has that, Is there a way to perhaps confirm that its the first version or something with your solution in mind?

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 17, 2017 @ 19:20
    Dan Diplo
    0

    That's a good point, but the Publishing event would run every time, too. You probably want to use the ContentService.Created event, in that case, which runs only once when the content is first created.

    private void ContentService_Created(IContentService service, NewEventArgs<IContent> e)
    {
        var doc = e.Entity;
    }
    
  • Ryan Noble 17 posts 85 karma points
    Mar 18, 2017 @ 09:12
    Ryan Noble
    0

    So it seems I have access during the publishing event, but running the content service query seems like a very time consuming thing to run.

    Is there a way to access TypedContent in this context?

    I would only be looking for already published modules so in theory I could just use the cached data instead of going to the DB for all the content.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Mar 18, 2017 @ 14:12
    Dan Diplo
    0

    You can't access published content until the content is actually published - so it won't be available in the Publishing event, which runs before, only the Published event.

    There is a way to access TypedContent, but as I say, it won't exist until published. You'd do something like:

    UmbracoHelper umbHelper = new UmbracoHelper(UmbracoContext.Current);
    
    IPublishedContent published = umbHelper.TypedContent(yourContent.Id);
    
  • Ryan Noble 17 posts 85 karma points
    Mar 18, 2017 @ 15:16
    Ryan Noble
    0

    So what I did was I passed the helper through into the event handler and then accessed typed content with that.

    So now it's all sorted :)

Please Sign in or register to post replies

Write your reply to:

Draft