Hello, the code below allows you to hook into a published article. I am not sure how I can access the title of the article for example. I should be able to do node.getTitle. I have been looking through the documentation, I cannot find examples. Any help would be appreciated.
public class SubscribeToContentPublishedNotifications :
INotificationHandler<ContentPublishedNotification>
{
public void Handle(ContentPublishedNotification notification)
{
foreach (var node in notification.PublishedEntities)
{
}
}
}
The PublishedEntities is a IContent list. This is not IPublishedContent. You can get value's from IContent but this is not the published version.
You can inject the IUmbracoHelperAccessor to get the published version.
if (!_umbracoHelperAccessor.TryGetUmbracoHelper(out var helper))
{
return;
}
foreach (var node in notification.PublishedEntities)
{
var publishedContent = helper.Content(node.Id);
if (publishedContent is null)
{
continue;
}
var titleAlias = publishedContent.Value<string>("yourTitleAlias");
}
Another thing you can do is try to cast it to its type and get the property strongly typed. Two advantages are that you'll only do something if it is the type you want, and if something changes your code won't compile so you are notified of broken code.
if (publishedContent is HomePage homePage)
{
var title = homePage.Title;
}
Where HomePage is the ModelsBuilder generated class.
This article should take you through the basics of creating a parent article page to list the published articles and the child items that will be listed and how to query through the entire list to list them and grab their title and contents.
How to return contents of a published Article
Hello, the code below allows you to hook into a published article. I am not sure how I can access the title of the article for example. I should be able to do node.getTitle. I have been looking through the documentation, I cannot find examples. Any help would be appreciated.
Hi,
The
PublishedEntities
is aIContent
list. This is notIPublishedContent
. You can get value's fromIContent
but this is not the published version.You can inject the
IUmbracoHelperAccessor
to get the published version.Another thing you can do is try to cast it to its type and get the property strongly typed. Two advantages are that you'll only do something if it is the type you want, and if something changes your code won't compile so you are notified of broken code.
Where
HomePage
is the ModelsBuilder generated class.Hello Roy,
Thanks for your response. I am struggling to make your suggestions work for me. Where is the "helper" variable declared from.
Also, would you know anywhere with good tutorials, on extracting data from published events, like I am trying to do?
Hi,
When doing such things, you need to be aware of: #13339
More you can find in the article: How to access currently published content in Umbraco
Best!
Hey there,
This article should take you through the basics of creating a parent article page to list the published articles and the child items that will be listed and how to query through the entire list to list them and grab their title and contents.
Here is the answer to my question. In C# that is how you can programmatically return values after hooking into the content created nortifications!
is working on a reply...