Copied to clipboard

Flag this post as spam?

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


  • Peter Dyhrberg 7 posts 81 karma points
    Nov 19, 2021 @ 08:42
    Peter Dyhrberg
    2

    How to get content from Id in Umbraco 9

    In the new Umbraco 9 I am trying to get the page content from node ID/Page Id in a class.

    I the old Umbraco 8 it could be made using the syntax:

    IPublishedContent currentNode = Umbraco.Content(123);
    

    Does anybody know how to do it in Umbraco 9?

    I have tried the below but without any luck:

    public static IPublishedContentQuery _publishedContentQuery;
    IPublishedContent content = _publishedContentQuery.Content(123);
    

    /Best regards Peter

  • Ambert van Unen 175 posts 819 karma points c-trib
    Nov 19, 2021 @ 11:27
    Ambert van Unen
    3

    Hi Peter,

    try injecting the IPublishedContentQuery like this

    public class Example { private readonly IPublishedContentQuery _publishedContentQuery;

        public Example(IPublishedContentQuery publishedContentQuery)
        {
            _publishedContentQuery = publishedContentQuery;
        }
    
        public IPublishedContent GetContentExample(int nodeId)
        {
            var node = _publishedContentQuery.Content(nodeId)
    
    
            return node;
        }
    
    }
    

    This code should work, atleast it does here ;-)

    It's basically the same as described here: https://our.umbraco.com/Documentation/Reference/Management/Services/ContentService/

  • Peter Dyhrberg 7 posts 81 karma points
    Nov 19, 2021 @ 12:53
    Peter Dyhrberg
    0

    Hi Ambert, thank you so much for your suggestion. I really appreciate it. I had already tried the method described in the link you have sent.

    I also right now tried the exact thing as you have recommended ... I could not make it work following your example unfortunately, as it always returned "null" (see image)

    enter image description here

    But I found another method that works by calling Services.ContentService:

    var test = Services.ContentService.GetById(pageId);
    var pageType = test.ContentType.Alias;
    

    / Best regards Peter

  • Ambert van Unen 175 posts 819 karma points c-trib
    Nov 19, 2021 @ 13:26
    Ambert van Unen
    0

    Hmm thats odd, it says the publishedContentQuery itself is null.

    Perhaps something went wrong with the dependencyInjection itself in the above code of your screenshot?

  • Peter Dyhrberg 7 posts 81 karma points
    Nov 19, 2021 @ 14:06
    Peter Dyhrberg
    0

    Yeah, it is strange Ambert.

    It is definitely because I don't get the Injection right in the Class-file. I just don't know what I am doing wrong.

    It worked on my Controller where I initiate the controller like this:

    public ContentSurfaceController(
                IUmbracoContextAccessor umbracoContextAccessor,
                IUmbracoDatabaseFactory databaseFactory,
                ServiceContext services,
                AppCaches appCaches,
                IProfilingLogger profilingLogger,
                IPublishedUrlProvider publishedUrlProvider)
                : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
            {
            }
    

    then I got the content like this, easy peasy: var test = Services.ContentService.GetById(pageId); var pageType = test.ContentType.Alias;

    But if I make a class-file (CommentsDataModel.cs) and try the same method then services are null (just like publishedContentQuery )

    I tried to do like this in my class-file:

    public readonly ServiceContext services;
        public IContent GetContentExample(int nodeId)
        {
            var node = services.ContentService.GetById(nodeId);
            return node;
        }
    

    But services are null here... Don't know why. Maybe I have to send "services" as a parameter when I call the class from my Controller to avoid services to be null?

    Thanks Ambert for your help so far / Best regards Peter

  • Ambert van Unen 175 posts 819 karma points c-trib
    Nov 19, 2021 @ 14:17
    Ambert van Unen
    0

    Hmm I'm not 100% sure if I follow you, and if you tried it exactly Like I did:

    public class Example { 
        private readonly IPublishedContentQuery _publishedContentQuery;
    
        public Example(IPublishedContentQuery publishedContentQuery)
        {
            _publishedContentQuery = publishedContentQuery;
        }
    
       public IPublishedContent GetContentExample(int nodeId)
       {
          var node = _publishedContentQuery.Content(nodeId)
    
    
          return node;
    }
    

    }

    does this work?

Please Sign in or register to post replies

Write your reply to:

Draft