I'm able to use Umbraco Helper to grab pages as IPublishedContent either by ID or by GUID. As I'm looking at the documentation, it appears to me I should be able to grab media as IPublishedContent in a very similar way:
But GetPdfById and GetPdfByGuid most certainly return null. I don't know that I've struggled with Umbraco like this before. I feel like I'm pretty competent within Umbraco. This feels so basic. What the heck am I missing here?
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Web.Common;
namespace UmbracoWebsite
{
public class TestUmbracoHelperModel
{
public IPublishedContent GetPageById { get; set; }
public IPublishedContent GetPageByGuid { get; set; }
public IPublishedContent GetPdfById { get; set; }
public IPublishedContent GetPdfByGuid { get; set; }
}
public class TestUmbracoHelperViewComponent : ViewComponent
{
private readonly UmbracoHelper _umbracoHelper;
public TestUmbracoHelperViewComponent(UmbracoHelper umbracoHelper)
{
_umbracoHelper = umbracoHelper;
}
public IViewComponentResult Invoke()
{
TestUmbracoHelperModel TestUmbracoHelperModel = new()
{
GetPageById = _umbracoHelper.Content(1831),
GetPageByGuid = _umbracoHelper.Content("6efa752a-1498-46c2-8b69-a5ea6630b274"),
GetPdfById = _umbracoHelper.Media(2414),
GetPdfByGuid = _umbracoHelper.Media("60ff3c64-535f-463c-8c64-6b16cf7f70fd")
};
return View($"/Views/Shared/Components/TestUmbracoHelper/TestUmbracoHelper.cshtml", TestUmbracoHelperModel);
}
}
}
The code has actually worked all along - but only for images. I updated it use the ID of an image and it works just fine. Problem is, I'm only really interested in PDFs, not JPGs.
Any insight on fetching PDFs by ID? Maybe they don't correlate to IPublishedContent?
I found that when I couldn't use UmbracoHelper.Media(Id) to fetch an UmbracoMediaArticle - and I still don't quite know what caused it - but re-saving that media item in Umbraco snapped it into place.
Most of the articles I was trying to fetch/test against had been added in a shared dev site then I pulled them down to my local environment to add some functionality. Maybe that contributed?
The functionality has to do with includin UmbracoMediaArticles in search results. And to do so they will have to toggle a Boolean on for "include in seach results". So any PDF they want showing up is going to have to be re-saved anyway.
UmbracoHelper to fetch media by ID
I'm able to use Umbraco Helper to grab pages as IPublishedContent either by ID or by GUID. As I'm looking at the documentation, it appears to me I should be able to grab media as IPublishedContent in a very similar way:
https://docs.umbraco.com/umbraco-cms/reference/querying/umbracohelper
But GetPdfById and GetPdfByGuid most certainly return null. I don't know that I've struggled with Umbraco like this before. I feel like I'm pretty competent within Umbraco. This feels so basic. What the heck am I missing here?
try using IPublishedContentQuery rather than UmbracoHelper and then use
For simplicity, you could also change this
to
The viewcomponent already knows your view should be in
/Views/Shared/Components/TestUmbracoHelper
The code has actually worked all along - but only for images. I updated it use the ID of an image and it works just fine. Problem is, I'm only really interested in PDFs, not JPGs.
Any insight on fetching PDFs by ID? Maybe they don't correlate to IPublishedContent?
I found that when I couldn't use UmbracoHelper.Media(Id) to fetch an UmbracoMediaArticle - and I still don't quite know what caused it - but re-saving that media item in Umbraco snapped it into place.
Most of the articles I was trying to fetch/test against had been added in a shared dev site then I pulled them down to my local environment to add some functionality. Maybe that contributed?
The functionality has to do with includin UmbracoMediaArticles in search results. And to do so they will have to toggle a Boolean on for "include in seach results". So any PDF they want showing up is going to have to be re-saved anyway.
is working on a reply...