Copied to clipboard

Flag this post as spam?

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


  • Frank Buchner 1 post 71 karma points
    Mar 19, 2019 @ 13:51
    Frank Buchner
    0

    Umbraco Content Service GetChildrenByName

    I need to access all of the child nodes of a node and get the one with a matching name.

    In Umbraco 7, GetChildrenByName() made this very useful, however it seems this is deprecated in umbraco 8.

    IContentService contentService = Services.ContentService; var node = contentService.GetChildrenByName(parentNodeId, nodeObject.nodeName).FirstOrDefault();

    I assume I could access all of the child nodes of the parent node, loop through them and match the name, but how would I then access that nodes content via the contentservice?

    There is no method like GetContent(int id).

    How can I do this in umbraco 8?

  • Simon Andrews 17 posts 131 karma points
    Mar 19, 2019 @ 17:27
    Simon Andrews
    0

    If you add a ServiceContext to your constructor you can use the dependency injection to do something like this:

    public class MyClass 
    {
        protected ServiceContext Services { get; }
    
        public MyClass (ServiceContext services)
        {
            Services = services;
        }
    
        public void MyMethod(int contentId)
        {
            var content = Services.ContentService.GetById(contentId)  as IPublishedContent; // cast to IPublishedContent to get the children collection
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft