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 122 posts 506 karma points
    Jul 13, 2017 @ 09:33
    Tom Bruce
    0

    Find the url of Image

    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 21 posts 134 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 122 posts 506 karma points
    Jul 17, 2017 @ 16:08
    Tom Bruce
    0

    Thanks Rob :)

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    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