Copied to clipboard

Flag this post as spam?

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


  • umbracoKnee 4 posts 75 karma points
    Oct 12, 2022 @ 11:03
    umbracoKnee
    0

    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.

    public class SubscribeToContentPublishedNotifications : 
    INotificationHandler<ContentPublishedNotification>
     {
      public void Handle(ContentPublishedNotification notification)
         {
    
            foreach (var node in notification.PublishedEntities)
            {
            }
         }
      }
    
  • Roy Berris 89 posts 577 karma points c-trib
    Oct 13, 2022 @ 11:06
    Roy Berris
    1

    Hi,

    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.

  • umbracoKnee 4 posts 75 karma points
    Oct 13, 2022 @ 16:30
    umbracoKnee
    0

    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?

  • Wojciech Tengler 96 posts 202 karma points
    Feb 02, 2024 @ 11:22
    Wojciech Tengler
    1

    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!

  • Phung Dao 22 posts 102 karma points
    Oct 18, 2022 @ 22:09
    Phung Dao
    0

    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.

  • umbracoKnee 4 posts 75 karma points
    Oct 18, 2022 @ 23:48
    umbracoKnee
    1

    Here is the answer to my question. In C# that is how you can programmatically return values after hooking into the content created nortifications!

     foreach (var node in notification.PublishedEntities)
            {
    
                var theTitle = node.GetValue<string>("articlesTitle");
                var text= node.GetValue<string>("articlesBodyText");
                Console.WriteLine(theTitle);
                Console.WriteLine(text);
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft