Copied to clipboard

Flag this post as spam?

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


  • Henrik Vincent 122 posts 616 karma points
    Jul 31, 2016 @ 11:48
    Henrik Vincent
    0

    List items from specific node globally

    Hi there

    I'm trying to create a marquee with a list of items, that should be displayed on all pages.

    My struture

    enter image description here
    WIthin each chillditem there's a mediapicker with marqueeLogo and a textfiled with marqueeLogoAlt

    How I tried to accomplish this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{ var selection = CurrentPage.Site().Kunder.Children.Where("Visible"); }
    
    <ul>
        @foreach (var id in selection)
        {
            var item = Umbraco.Content(id);
            var marqueeItem = Umbraco.Media(@item.marqueeLogo); 
            <li class="slideItem">
                    <img src="@marqueeItem.umbracoFile" alt='@item.marqueeLogoAlt'/> 
                </div>
            </li>
        }
    </ul>
    

    I'm guessing that it's my selection variable that doesn't point correct.
    Also tried root.Kunder.Children and root.Descendents("Kunder")

    I'm pretty new to the new Umbraco, so not really sure how to point it correctly, so they will be shown on all pages.

    Thanks in advance
    Henrik

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 31, 2016 @ 12:39
    Alex Skrypnyk
    1

    Hi Henrik,

    I don't recommend you to use CurrentPage object and other dynamic helpers, especially if you just started to work with Umbraco, it will be better for you to start from strongly typed approach.

    I rewrite your macro like that:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{
        var kunderSection = Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.Name == "Kunder");
    }
    
    <ul>
        @foreach (var item in kunderSection.Children)
        {
            var marqueeItem = Umbraco.TypedMedia(item.GetPropertyValue("marqueeLogo"));
    
            <li class="slideItem">
                <img src="@marqueeItem.Url" alt="@marqueeItem.GetPropertyValue("marqueeLogoAlt")" />
            </li>
        }
    </ul>
    

    1) Inheriting from UmbracoViewPage gives you strongly typed model of view without dynamics

    2) Please change this line - var kunderSection = Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.Name == "Kunder");

    I used selection by name, because don't know doctype of Kunder node, better will be use it like :

    var kunderSection = Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == "KunderDocumentTypeAlias");
    

    3) Do you want to create macro for using it in all site? You have to use

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    

    And place your macro view in ~/Views/MacroPartials, read more https://our.umbraco.org/documentation/reference/templating/macros/partial-view-macros/

    I hope it will help you, write if something.

    Thanks,

    Alex

  • Henrik Vincent 122 posts 616 karma points
    Jul 31, 2016 @ 13:29
    Henrik Vincent
    1

    Hi Alex and thank you very much for your informative suggestion.

    I tried creating a macro instead called ClientMarquee which is located in ~/Views/MacroPartials/ClientMarquee.cshtml

    I'm using the code from your suggestion, but when trying to render the macro I get this:

    Error loading Partial View script (file: ~/Views/MacroPartials/ClientMarquee.cshtml)

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var kunderSection = Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == "Kunder");
    }
    
    <ul>
        @foreach (var item in kunderSection.Children)
        {
            var marqueeItem = Umbraco.TypedMedia(item.GetPropertyValue("marqueeLogo"));
    
            <li class="slideItem">
                <img src="@marqueeItem.Url" alt='@marqueeItem.GetPropertyValue("marqueeLogoAlt")' />
            </li>
        }
    </ul>
    

    The docType is Kunder as you assumed, so not sure where I'm going wrong.

    Henrik

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 31, 2016 @ 13:42
    Alex Skrypnyk
    100

    Hi Henrik,

    1) Added few checks for null, now code looks like:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @{
        var kunderSection = Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == "Kunder");
    }
    
    @if (kunderSection != null)
    {
        <ul>
            @foreach (var item in kunderSection.Children)
            {
                var marqueeItem = Umbraco.TypedMedia(item.GetPropertyValue("marqueeLogo"));
    
                if (marqueeItem != null)
                {
                    <li class="slideItem">
                        <img src="@marqueeItem.Url" alt='@marqueeItem.GetPropertyValue("marqueeLogoAlt")'/>
                    </li>
                }
            }
        </ul>
    }
    

    2) Also be carefull with DocumentTypeAlias, check it, it's often lowercase so maybe document type alias is "kunder" and you need to change to

    x.DocumentTypeAlias == "kunder"
    

    Please check in your Umbraco.

    Thanks,

    Alex

  • Henrik Vincent 122 posts 616 karma points
    Jul 31, 2016 @ 14:13
    Henrik Vincent
    0

    Hi again Alex

    Now it's working perfectly.

    Thanks you very, very much for your help!

    I really appreciate it

    Best
    Henrik

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 31, 2016 @ 14:16
    Alex Skrypnyk
    1

    Maybe problem with @marqueeItem.GetPropertyValue("marqueeLogoAlt")

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var kunderSection = Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias == "Kunder");
    }
    
    @if (kunderSection != null)
    {
        <ul>
            @foreach (var item in kunderSection.Children)
            {
                var marqueeItem = Umbraco.TypedMedia(item.GetPropertyValue("marqueeLogo"));
    
                if (marqueeItem != null)
                {
                    string altText = string.Empty;
                    if (marqueeItem.HasValue("marqueeLogoAlt"))
                    {
                        altText = marqueeItem.GetPropertyValue<string>("marqueeLogoAlt");
                    }
    
                    <li class="slideItem">
                        <img src="@marqueeItem.Url" alt="@altText"/>
                    </li>
                }
            }
        </ul>
    }
    

    Added check for altText property.

    Thanks,

    Alex

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 31, 2016 @ 14:17
    Alex Skrypnyk
    1

    You are welcome !!! I'm glad to help.

    Best,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft