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 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?
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.
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);
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?
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 inUmbraco.Core
eg.Example:
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?
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.
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.
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:
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 :)
is working on a reply...