Copied to clipboard

Flag this post as spam?

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


  • Brett Palmer 8 posts 98 karma points
    Sep 29, 2023 @ 03:47
    Brett Palmer
    0

    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?

    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);
            }
        }
    }
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Sep 29, 2023 @ 09:09
    Huw Reddick
    0

    try using IPublishedContentQuery rather than UmbracoHelper and then use

    _publishedContentQuery.Media(2975);
    
  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Sep 29, 2023 @ 09:15
    Huw Reddick
    0

    For simplicity, you could also change this

    return View($"/Views/Shared/Components/TestUmbracoHelper/TestUmbracoHelper.cshtml", TestUmbracoHelperModel);
    

    to

    return View($"TestUmbracoHelper", TestUmbracoHelperModel);
    

    The viewcomponent already knows your view should be in /Views/Shared/Components/TestUmbracoHelper

  • Brett Palmer 8 posts 98 karma points
    Sep 30, 2023 @ 20:21
    Brett Palmer
    0

    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?

  • Brett Palmer 8 posts 98 karma points
    Oct 01, 2023 @ 12:19
    Brett Palmer
    100

    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.

Please Sign in or register to post replies

Write your reply to:

Draft