Copied to clipboard

Flag this post as spam?

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


  • Meni 281 posts 539 karma points
    Jan 26, 2023 @ 07:07
    Meni
    0

    Migrate to Umbraco 11.1: How to get nodes, then render images (@Umbraco.Media)

    Hi, I'm working on migrating my Umbraco 7.15.7 to 11.1

    I tried to render the images without success. I looked in the documentation, forum, etc. but couldn't figure out.

    Here's my code in 7.15.7:

    It's a partial view macro file where first I'm getting the nodes for the section

    var Imaging_node = Umbraco.Content(1234);    
    
    var Imaging_pagesToList = @Imaging_node.Children.Where("Visible");
    Imaging_pagesToList = Imaging_node.DescendantsOrSelf().Where("NodeTypeAlias == @0", "NewsPage");
    

    Then I start display the images in few places, for example:

    @if (Imaging_pagesToList.Last().HasValue("articleImg"))     
                    {
                        <img src="@Umbraco.Media(Imaging_pagesToList.Last().articleImg.ToString()).Url" class="img-fluid" alt="@Imaging_pagesToList.Last().photoAlt" />
                    }
    

    My questions are:

    Question 1:

    How can I get the nodes?

    @Imaging_node.Children.Where("Visible")
    

    and

    DescendantsOrSelf().Where("NodeTypeAlias == @0", "NewsPage");
    

    are not working and give me errors.

    What I was able to do is the following , but I'm not sure if it's the right way:

    var Imaging_pagesToList = @Imaging_node.Children.Where(x => x.IsVisible());
    Imaging_pagesToList = Imaging_node.DescendantsOrSelf().OfTypes("NewsPage");
    

    Question 2:

    How do I render the images??

    I tried many things, looked in the documentation, etc. and couldn't figure out.

    This is (again) how I do it in my 7.15 website:

    <img src="@Umbraco.Media(Imaging_pagesToList.Last().articleImg.ToString()).Url" class="img-fluid" alt="@Imaging_pagesToList.Last().photoAlt" />
    

    where articleImg is a property in the document types that display the page. and photoAlt is a property to show the image description

    Thanks for the help :)

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 9x admin c-trib
    Jan 26, 2023 @ 14:47
    Alex Skrypnyk
    1

    Hi Meni,

    Use this code:

    var Imaging_pagesToList = @Imaging_node.Children.Where(n => n.IsVisible());
    Imaging_pagesToList = Imaging_node.DescendantsOrSelf().Where(n => n.ContentType.Alias == "NewsPage");
    

    Thanks, Alex

  • Meni 281 posts 539 karma points
    Jan 26, 2023 @ 15:58
    Meni
    0

    Thanks.

    What about images? How do I render images in Umbraco 11?

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 9x admin c-trib
    Jan 26, 2023 @ 17:09
    Alex Skrypnyk
    100

    Something like this:

    <img src="@Umbraco.Media(Imaging_pagesToList.Last().Value<IPublishedContent>("articleImg").ToString()).Url()" class="img-fluid" alt="@Imaging_pagesToList.Last().Value("photoAlt")" />
    
  • Meni 281 posts 539 karma points
    Jan 27, 2023 @ 05:50
    Meni
    0

    Thanks, this works.

    Quick question: I tried also this (before you answered me) and this is also works:

    Imaging_pagesToList = Imaging_node.DescendantsOrSelf().OfTypes("NewsPage");
    

    Is it also a correct way?

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 9x admin c-trib
    Jan 27, 2023 @ 12:09
    Alex Skrypnyk
    0

    yes, but it's better to use strongly typed variables

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies