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)
But I found another method that works by calling Services.ContentService:
var test = Services.ContentService.GetById(pageId);
var pageType = test.ContentType.Alias;
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
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;
}
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:
Does anybody know how to do it in Umbraco 9?
I have tried the below but without any luck:
/Best regards Peter
Hi Peter,
try injecting the IPublishedContentQuery like this
public class Example { private readonly IPublishedContentQuery _publishedContentQuery;
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/
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)
But I found another method that works by calling Services.ContentService:
/ Best regards Peter
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?
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:
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:
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
Hmm I'm not 100% sure if I follow you, and if you tried it exactly Like I did:
}
does this work?
is working on a reply...