Copied to clipboard

Flag this post as spam?

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


  • John Beatrice 5 posts 92 karma points
    Aug 28, 2018 @ 21:41
    John Beatrice
    0

    Umbraco 4.11.8 Related Links in Macro Scripting File (cshtml)

    Hello,

    (yes, a very old installation and using Webforms)

    Related Links in Umbraco 4.11.8 inside of a macro scripting file works nicely when I specify the node ID containing the related links, inside of the macro parameter. However, I need to access the related links properties by traversing the tree instead of a macro parameter.

    The following works well. But, I need to do something differently now.

    my.master uses:

    <umbraco:Macro startNode="2605" Alias="FooterLinks" runat="server"></umbraco:Macro>
    

    and the macro's razor file iterates through the Related Links:

    @{
        var startNodeID = Parameter.startNode;
    }
    
    @if(startNodeID != null)
        {
            var startNode = Model.NodeById(startNodeID);
            if(startNode.footerLinks.ToString().Length > 2)
            {
                foreach(var item in startNode.footerLinks)
                {
                    var url = item.type == "internal" ? Library.NodeById(item.link).Url : item.link;
                    if (!item.newwindow.Equals("1")) {
                        url = url.Replace("https://","").Replace("http://","");
                    }
                    var target = item.newwindow.Equals("1") ? "_blank" : string.Empty;
                    var title = item.title;
                    <a class="foot-link" href="@url" target="@target">@title</a>
                }
            }
        }
    

    If there are several related link collections in the node, the above example also works nicely to iterate through those...

    But, I need to eliminate the "startNode" parameter and access the Related Links using the "FooterLinks" Document Type node alias from the content tree:

    Content

    .Home
    ..page
    ..page2
    ..etc

    .FooterLinks < ---- grab this by document type alias and iterate through related links properties.

    The below code will find the related links node by document type alias and print the XML to screen:

    @{
        var startNode = umbraco.uQuery.GetNodesByType("FooterLinks");
    }
    
    @if(startNode != null)
    {
        foreach(var collection in startNode)
        {
            // collection is "umbraco.NodeFactory.Node"
            // links1 is a Related Links alias
            foreach(var items in collection.GetProperty("links1").Value)
            {
                // How to access the Related Links properties? 
                // Umbraco 4.11.8 will not allow this: foreach( var item in items) { @item.GetProperty("title").Value }
    
                    @items.ToString() // this will print the XML
                    @* <links><link title="My Cool Page Link" link="1053" type="internal" newwindow="0"><link title="Another Page Link" link="1073" type="internal" newwindow="0"><link title="Third Link" link="1456" type="internal" newwindow="0"></links>               *@  
            }
        }
    }
    

    I'm completely stuck. Do I use Newsoft and convert to JSON and then access properties?

    Thanks in advance!

  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Aug 28, 2018 @ 21:51
    Damiaan
    0

    It is on top of my head. But if you look at the Umbraco v4 cheat sheet, you have a AncestorOrSelf.

    So that would be something like Model.AncestorOrSelf(1).Descendants("docTypeAliasOfTheFooterLinksPage").GetProperty("propertyWithTheLinks").

    Take care, descendants is a very slow on big sites. If you have lots of nodes, it's (very) bad practice. But you could do GetChildrenAsList and then loop through them.

    Hope it helps!

    Damiaan

  • Rami H 13 posts 141 karma points
    Aug 28, 2018 @ 22:41
    Rami H
    100

    Hey old friend,

    Using uQuery, you're getting back a type of Node instead of DynamicNode. If you want to use the dot notation, I think returning the same type of DynamicNode should be good.

    In your first example try switching var startNode = Model.NodeById(id) for Model.XPath("//documentTypeAlias"). Model.XPath() should give you collection of everything with the passed in alias. Then just grab the first one.

    //This should return first DynamicNode in collection
    var startNode = Model.XPath("//FooterLinks").FirstOrDefault();
    

    Then the rest of the code from the first example should be the same! Otherwise, your option is to parse out the xml into something usable if you want to continue using uQuery.

    Here's the referenced docs between the two querying methods in case you need to dig deeper. Umbraco 4 Querying

    Hope all is well :)

  • John Beatrice 5 posts 92 karma points
    Aug 29, 2018 @ 13:59
    John Beatrice
    0

    Rami thank you dear friend!

    var startNode = Model.XPath("//FooterLinks").FirstOrDefault();
    

    Worked perfectly!

    Damiann: thanks for your help! I tried doing that same thing the other day and for some reason I kept running into problems.

    Solved! Thanks again

  • Rami H 13 posts 141 karma points
    Aug 29, 2018 @ 14:14
    Rami H
    0

    No worries, you're welcome anytime.

    Sometimes the answer is much simpler than the proposed problem point :) You gave ample information on the issue and what you were trying to accomplish. So many OPs end up with missing information!

    Take care man.

Please Sign in or register to post replies

Write your reply to:

Draft