Copied to clipboard

Flag this post as spam?

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


  • Josh Bula 13 posts 75 karma points
    Apr 18, 2017 @ 17:44
    Josh Bula
    0

    Umbraco 7.6 RC Related Links - 'Umbraco.Web.Models.RelatedLink' does not contain a definition for 'link' or for 'isInternal'

    I have a list of document types called newsItems I'm trying to display in a macro partial. They have a relatedLinks data type. I'm trying to display a simple list of the related links for each of the newsItems queried. I have successfully done this using similar code in previous versions of Umbraco but for some reason it's not working in a new site I'm developing using 7.6.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var query = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where("Visible && DocumentTypeAlias == @0", "newsItem").OrderBy("sortOrder desc");
    
        foreach (var page in query)
        {
            <h1>@page.Headline</h1>
            <p>@page.body</p>
    
    
            if (page.HasValue("relatedLinks") && page.relatedLinks.ToString().Length > 2)
            {
                foreach (var item in page.relatedLinks)
                {
                    var linkUrl = (bool)item.isInternal ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.link;
                    var linkTarget = (bool)item.newWindow ? "_blank" : null;
                    <p><a href="@linkUrl" target="@linkTarget">@item.caption</a></p>
                }
            }
        }
    }
    

    This line is trowing the error 'Umbraco.Web.Models.RelatedLink' does not contain a definition for 'isInternal'

    var linkUrl = (bool)item.isInternal ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.link;
    

    If I take out the contitional and just do var linkUrl = item.link it says it doesn't containg a definition for 'link'

    Have I overlooked something? or is there a different recommended method of doing something in 7.6?

  • Josh Bula 13 posts 75 karma points
    Apr 18, 2017 @ 18:14
    Josh Bula
    0

    If I switch from Dynamic to Typed, I get a completely different problem that I posted about here.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 19, 2017 @ 06:17
    Dave Woestenborghs
    102

    Hi Josh,

    I guess you started this site on v7.6. In this version Umbraco switched to the "UDI" format for storing links. For content these start with "umb://document/" and for media with "umb://media"

    From your code I see you are trying to parse the json your self. But in v7.6 this wonderful package (https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/) by Jeavon Leopold got integrated in the core.

    So you can do this in your view : var relatedLinks = Model.GetPropertyValue

    Also I see you are using the dynamic syntax in your views. I would advise you to use the strongly typed version : http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/

    Since you are using v7.6 the modelsbuilder is activated by default and you can use strongly typed models in your view. You can read some more about the modelsbuilder here : http://24days.in/umbraco-cms/2016/getting-started-with-modelsbuilder/

    Dave

  • Josh Bula 13 posts 75 karma points
    Apr 19, 2017 @ 13:43
    Josh Bula
    0

    Thanks Dave, I was able to get it working that way. The links you provided were very helpful.

    Now that I have that relatedLinks object, I see that it has the properties Caption, NewWindow, IsInternal, Type, and Link, and I was able to successfully use those within a foreach loop.

    Is there any documentation somewhere that shows other ways to work with these types of objects? Specifically, I'm trying to get a count of the related links, and display only the first link. I have tried options typical of lists or IENumerables such as relatedLinks.Count() and relatedLinks[0] and relatedLinks.Any() and just get errors saying Umbraco.Web.Models.RelatedLinks does not contain those definitions.

    Thanks!

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Apr 19, 2017 @ 13:49
    Dave Woestenborghs
    0

    Hi Josh,

    This is the source code for the RelatedLinks object : https://github.com/umbraco/Umbraco-CMS/blob/dev-v7.6/src/Umbraco.Web/Models/RelatedLinks.cs

    As you can see it implements/inherits IEnumerable<RelatedLink>

    Maybe adding the following using statement to your view will solve the issue:

    using System.Linq
    

    Don't forget the post as a solution. This is new in 7.6 a lot of people will run into this issue and it's easier for them to find the solution.

    Dave

  • Josh Bula 13 posts 75 karma points
    Apr 19, 2017 @ 14:33
    Josh Bula
    1

    Thanks! I got it working by adding these using statements:

    @using System.Linq;
    @using Umbraco.Web.Models;
    

    and changing

     var links = page.GetPropertyValue("relatedLinks");
    

    to this:

     IEnumerable<RelatedLink> links = page.GetPropertyValue("relatedLinks");
    

    Thanks again for your help! ...Josh

  • Sigurd Aabøe-Sagen 12 posts 122 karma points
    May 09, 2017 @ 10:36
    Sigurd Aabøe-Sagen
    0

    This should be marked as a solution.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    May 09, 2017 @ 10:38
    Dave Woestenborghs
    0

    It already is.

    Dave

  • 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