IContent or IPublishedContent?
# help-with-umbraco
t
Hi I have a controller/class where a method in this class is being called to get a page programmatically. As an example, in Umbraco 13 I have 2 pages. When I'm on page 2, I'm calling this method to get some details from page 1. I can get it by IContent (GetById) or some documents suggest using IPublishedContent. Which is the correct way to get this type of record? Thanks
s
You should only use
IPublishedContent
unless you need data straight from the database and understand the performance hit this causes. https://dev.to/mattbrailsford/converting-icontent-to-ipublishedcontent-in-umbraco-v8-3m7i#:~:text=The%20main%20difference%20with%20these,accessible%20and%20strongly%20typed%20manor.
t
It was that link that led me to asking this question 😀 If using IPublishedContent then I don't see a method to get page by guid?
s
Yes, you can always get it by Guid for
IPublishedContent
. Show some code where you're stuck and we'll have a look.
t
I think I've worked it out. I added UmbracoHelper in my constructor and did: Var con = umbracHelper.Content(myGuid); This seems to have worked, is this the correct way to retrieve the page contents?
s
that looks about right yeah, as long as it gives you an
PublishedContent
! one tip, if you already have a content item, then
content.Key
will give you the
Guid
of that item. I don't know if that's the case for you but it might be useful.
146 Views