Copied to clipboard

Flag this post as spam?

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


  • Tim 25 posts 112 karma points
    May 13, 2017 @ 19:24
    Tim
    0

    I'm having major failures trying to update my Macros, could use direction

    So at the recommendation of Jeavon ( https://our.umbraco.org/forum/templates-partial-views-and-macros/85678-umbracomedia-id-mediafile-not-working-on-76 ) I have started working on updating my Macros to the latest standards. These macros have been in place since 4.x I believe.

    This is my 'children' menu macro, pulls the node assigned through Parameter on the Macro and then iterates through the children and generates links based on Doctype and visible state (Set by a composition on all doctypes)

    So Home, LandingPage, ParentPage, SidePage are all Doctypes, each one has the NavigationPart in it's composition. I have started modifying the code already and this is as far as I made it.

    @using Umbraco.Web.Extensions
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
    
        Udi udi;
        Udi.TryParse(Model.MacroParameters["NodeID"].ToString(), out udi);
    
        var menuNode = udi.ToPublishedContent();
    
        validNodes.Add("landingpage");
        validNodes.Add("parentpage");
        validNodes.Add("sidepage");
        validNodes.Add("foldere");
        validNodes.Add("link");
    }
    
    <h2><a href="@menuNode.Url">@menuNode.Name</a></h2>
    <ul>
        @foreach(dynamic child in menuNode.Children)
        {
            @TraverseMenu(child)
        }
    </ul>
    
    @*----------------------------------------------------
     * TraverseMenu
     * Params:
     *     startNode: Node to begin at (will display)
     *     nodeId: Root NodeID (Only needs passed on the
     *             initial call
     *
     * Output:
     *     A ul/li list that represents the tree menu
     *--------------------------------------------------*@
    @helper TraverseMenu(dynamic node)
    {
        if(!node.Content.GetPropertyValue("hideFromSiteMap") & validNodes.Contains(node.DocumentTypeAlias.ToLower())) 
        {
            string documentType = node.DocumentTypeAlias.ToLower();
    
            switch(documentType)
            {
                case "landingpage":
                case "landingpagetabed":
                case "parentpage":
                case "foldere":
    
                    @:<li>@LinkForNode(node)
    
                    @:<ul>
    
                    foreach(dynamic child in node.Children()) {
    
                        @TraverseMenu(child)
                    }
    
                    @:</ul></li>
                    break;
    
                case "sidepage":
                case "link":
    
                    <li>@LinkForNode(node)</li>
                    break;
            }
        }
    }
    
    @*----------------------------------------------------
     * LinkForNode
     * Params:
     *     node: Node to generate link for
     *     customClass: Custom class that will be added to
     *                  the <a> tag
     *
     * Output:
     *     A formated <a> tag that points to the proper
     * URL based on children and it's Navigation options
     *--------------------------------------------------*@
    @helper LinkForNode(dynamic node, string name = "")
    {
        string documentType = node.DocumentTypeAlias.ToLower();
    
        if(name == "") {name = node.Name;}
    
        switch(documentType)
        {
            case "landingpage":
            case "landingpagetabed":
            case "parentpage":
            case "sidepage":
    
                <a href="@node.Url">@name</a>
                break;
    
            case "foldera":
            case "plainfolder":
    
                dynamic firstNode = GetFirstAvailable(node);
    
                if(firstNode != null) {
    
                    @LinkForNode(firstNode, node.Name)
                } else {
    
                    <a href="#">Error</a>
                }
                break;
    
            case "link":
    
                <a href="@node.LinkTarget">@name</a>
                break;
        }
    }
    
    @functions
    {
    
        List<string> validNodes = new List<string>();
    
    /*----------------------------------------------------
     * GetFirstAvailable
     * Params:
     *     node: Node to start search at
     *
     * Returns:
     *     First available Node that would be visible in
     * the menu system based on Navigation options set
     *--------------------------------------------------*/
        public dynamic GetFirstAvailable(dynamic node)
        {
            foreach (dynamic child in node.Children)
            {
                if(validNodes.Contains(child.DocumentTypeAlias.ToLower())) {
    
                    return child;
                }
            }
    
            return null;
        }
    }
    

    I'm not looking for someone to convert it, I'm looking for information. At Jeavons suggestion I purchased an Umbraco.TV sub and went through the Model builders stuff and I must be missing something. It now errors on this line:

    if(!node.Content.GetPropertyValue("hideFromSiteMap") & validNodes.Contains(node.DocumentTypeAlias.ToLower()))
    

    The error is:

    'Umbraco.Core.Models.PublishedContent.PublishedContentWrapped.Content' is inaccessible due to its protection level

    I tried !node.Content.GetPropertyValue and !node.GetPropertyValue and have no luck. The 'hideFromSiteMap' property is in the NavigationPart doctype.

    What am I doing wrong???

  • Tim 25 posts 112 karma points
    May 13, 2017 @ 23:07
    Tim
    1

    Finally figured out the answer.

    I switched from PureLive models to Dll models. Opened the site in Visual Studio (we generally only use the backoffice as it's installed on a clients webserver).

    Changed from using 'dynamic' to 'IPublishedContent' and started using GetPropertyValue<> to get properties.

    Hope that helps someone else!

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 25, 2017 @ 07:12
    Alex Skrypnyk
    0

    Hi Tim

    Its really cool that you changed from using 'dynamic' to 'IPublishedContent'

    It's what Umbraco wants from you ) Have a nice day!

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft