Copied to clipboard

Flag this post as spam?

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


  • Tom Bruce 43 posts 233 karma points
    Jul 13, 2017 @ 09:33
    Tom Bruce
    0

    Hi folks, how do you find image url?

    I have tried drilling down into the properties, I receive a null reference at the following code Udi tempImageUrl = mediaItem.Url;

    Full code below

     public ActionResult RenderFeatured()
        {
            List<FeaturedItem> model = new List<FeaturedItem>();
            IPublishedContent homePage = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "home").FirstOrDefault();
            ArchetypeModel featuredItems = homePage.GetPropertyValue<ArchetypeModel>("featuredItems");
    
            foreach (ArchetypeFieldsetModel fieldset in featuredItems)
            {
                Udi imageId = fieldset.GetValue<Udi>("image");
                var mediaItem = Umbraco.Media(imageId);
    
                Udi tempImageUrl = mediaItem.Url;
                string imageUrl = tempImageUrl.ToString();
    
  • Rob Carlaw 17 posts 129 karma points
    Jul 13, 2017 @ 11:34
    Rob Carlaw
    100

    Does that node have a image/ is the node publishd?

    Put a breakpoint and step through it.

    try something like:

    string imageId = fieldset.GetValue<string>("image");
    var mediaItem = Umbraco.Media(imageId);
    
  • Tom Bruce 43 posts 233 karma points
    Jul 17, 2017 @ 16:08
    Tom Bruce
    0

    Thanks Rob :)

  • Alex Skrypnyk 3656 posts 12494 karma points mvp
    Jul 19, 2017 @ 13:23
    Alex Skrypnyk
    1

    Hi Guys

    Be aware that "Umbraco.Media" returns dynamic type object, it's better to use Umbraco.TpyedMedia()

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft