Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Jan 18, 2018 @ 15:55
    Craig O'Mahony
    0

    Render related links contained in Archetype 'section'

    Hi folks.

    I'm having trouble rendering a Related Links picker that's found in an Archetype section.

    To further explain I've got a Archetype with three properties, 2 textstrings and a related links picker. I can get the string values no problem but I cannot get the related links.

    What I've got so far is:

    var linksNode = new umbraco.NodeFactory.Node(dNode.Id);
    
    var links = JsonConvert.DeserializeObject<ArchetypeModel>(linksNode.GetProperty("drawerSection").Value);
    
    foreach (var link in links)
    {
        <p>@link.GetValue("drawerColumns")</p> //displays fine
        <p>@link.GetValue("drawerTitle")</p> //displays fine
    }
    

    If I use @link.Getvalue("drawerLinks") I see an enormous string that contained all of the related link data but try as I might I can't get 'inside' it to loop through all of the links.

    Any advice would be greatly received.

    Thanks, C

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jan 18, 2018 @ 20:52
    Alex Skrypnyk
    1

    Hi Craig

    You have to parse related links json for rendering it, try this code

    var array = JArray.Parse(link.GetValue("drawerLinks"));
    <ul>
                @foreach (var item in array)
                {
                    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                    var linkTarget = item.Value<bool>("newWindow") ? "_blank" : null;
                    <li><a href="@linkUrl" target="@linkTarget">@(item.Value<string>("caption"))</a></li>
                }
    </ul>
    
  • Craig O'Mahony 364 posts 918 karma points
    Jan 19, 2018 @ 09:13
    Craig O'Mahony
    0

    Hi Alex,

    Fantastic! I'm getting some errors on the linkUrl and linkTarget but I think that is down to the UDI / ID change in Umbraco.

    Thanks again, Craig

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jan 19, 2018 @ 09:26
    Alex Skrypnyk
    0

    Hi Craig

    What error? Lets have a look together

  • Craig O'Mahony 364 posts 918 karma points
    Jan 19, 2018 @ 09:40
    Craig O'Mahony
    0

    Hiya,

    The error is:

    'item.Value

    System.FormatException: 'Input string was not in a correct format.'

    item = {{ "caption": "Anti money Laundering", "link": "umb://document/a2fb93c35b6342548332aa130ea01ac0", "newWindow": false, "internal": "umb://document/a2fb93c35b6342548332aa130ea01ac0", "edit": false, "isInternal": true, "internalName": "UK"...

    These are the values contained within the item

    I'm resuming that I need to convert this UDI into an ID in order to be able to use the NiceUrl.

    Thanks, Craig

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Jan 19, 2018 @ 09:48
    Alex Skrypnyk
    0

    Hi Craig

    Try this line:

    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.TypedContent(Udi.Parse(item.Value<string>("internal"))).Url : Umbraco.TypedContent(Udi.Parse(item.Value<string>("link"))).Url;
    
  • Craig O'Mahony 364 posts 918 karma points
    Jan 19, 2018 @ 10:02
    Craig O'Mahony
    0

    Good work!

    Just had to change the external link clause as obviously Umbraco doesn't know what www.imdb.com is!

    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.TypedContent(Udi.Parse(item.Value<string>("internal"))).Url : item.Value<string>("link");
    

    Thanks for your time and assistance :)

    Thanks, C

  • 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