Copied to clipboard

Flag this post as spam?

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


  • kleptbit 42 posts 98 karma points
    May 18, 2017 @ 11:36
    kleptbit
    0

    Getting strongly typed Related Links in Umbraco V6

    Hello guys,

    I can't for the life of me get Related Links out of IPublishedContent in V6.0.6 (I know this is very old, I'm converting dynamics to strongly typed). Can anyone help me please? I've been googling for days.

    I know the property type is Umbraco.Web.Models.XmlPublishedContentProperty and I've got the string but I don't know what to parse it with.

    Sorry, this code is a mess, I know it won't work with JArray:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    

    @using Umbraco.Web @using Umbraco.Web.Models @using Newtonsoft.Json.Linq @{

    var thePage = Model.Content;
    var thePageID = thePage.Id;
    IPublishedContent theStronglyTypedPage = Umbraco.TypedContent(thePageID);
    var typedRelatedLinksConverted = Umbraco.TypedContent(Model.Content.GetPropertyValue<string>("relatedLinks").Split(','));
    var typedLinksList = typedRelatedLinksConverted.ToList();
    //var typedLinksArray = Umbraco.TypedContent(Model.Content.GetPropertyValue<JArray>("relatedLinks"));
    //var typedLinksString = Model.Content.Properties.Where(x => x.GetPropertyValue<string>("relatedLinks") == true);
    if (theStronglyTypedPage.HasProperty("relatedLinks") && theStronglyTypedPage.HasValue("relatedLinks"))
    {
        var i = 0;
       var x = theStronglyTypedPage.GetPropertyValue<string>("relatedLinks");
    
       foreach (var thing in Model.Content.GetPropertyValue<JArray>("relatedLinks"))
        {
            var linkUrl = (thing.GetPropertyValue<bool>("isInternal")) ? Umbraco.NiceUrl(thing.GetPropertyValue<int>("internal")) : thing.GetPropertyValue<string>("link");
            var linkTarget = thing.GetPropertyValue<bool>("newWindow") ? "_blank" : null;
            var linkType = (thing.GetPropertyValue<bool>("internal")) ? "book" : "external-link";
            <ul class="list-group" style="font-size:small">
            <li class="list-group-item active"><h3>Related links track external test</h3></li>
    
                <li><a href="@linkUrl" target="@linkTarget">@(thing.GetPropertyValue<string>("caption"))</a></li>
    
                <li class="list-group-item">
                    @if (linkType == "external-link")
                    {
                        <a href="@linkUrl" @Html.Raw(linkTarget)>@(thing.GetPropertyValue<string>("title"))&nbsp;<abbr class="ext" title="External website">ext</abbr></a>
                    }
                    else
                    {
                        <a href="@linkUrl" @Html.Raw(linkTarget)>@(thing.GetPropertyValue<string>("title"))&nbsp;</a>
                    }
                </li>
            }
        </ul>}
    <ul class="list-group" style="font-size:small">
        <li class="list-group-item active"><h3>Related links track external test</h3></li>
      for i = 0 to x.length
    
     <li>@x.ToString();</li>
    
    </ul>
    

    } }

    I keep going round in circles trying different things, getting different errors:

    `Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'title' and no extension method 'title' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)`
    

    foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    May 18, 2017 @ 13:34
    Alex Skrypnyk
    0

    Hi kleptbit

    Just installed Umbraco V6.0.6 ))

  • Craig Mayers 164 posts 508 karma points
    May 18, 2017 @ 13:43
    Craig Mayers
    0

    Hi,

    I have done this before, but I created my own custom logic for handling this.

    You will need a DTO and some logic to handle parsing the data returned from the RelatedLinks property.

    I will see if I can find my old code for you...

    Craig

  • kleptbit 42 posts 98 karma points
    May 18, 2017 @ 13:50
    kleptbit
    0

    Oh you guys, thank you! I had little hope of a response, because it's so old.

    The site is big, the client won't upgrade and performance is suffering badly, hence me trying to strongly type all the things...

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    May 18, 2017 @ 13:59
    Alex Skrypnyk
    0

    My version:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @using Newtonsoft.Json.Linq
    @{
    
        var thePage = Model.Content;
        var thePageID = thePage.Id;
        Umbraco.Core.Models.IPublishedContent theStronglyTypedPage = Umbraco.TypedContent(thePageID);
        var i = 0;
        var x = theStronglyTypedPage.GetPropertyValue<string>("relatedLinks");
    
        if (!string.IsNullOrWhiteSpace(x))
        {
            foreach (var thing in Model.Content.GetPropertyValue<JArray>("relatedLinks").ToList())
            {
                var linkUrl = (thing.Value<bool>("isInternal")) ? Umbraco.NiceUrl(thing.Value<int>("internal")) : thing.Value<string>("link");
                var linkTarget = thing.Value<bool>("newWindow") ? "_blank" : null;
                var linkType = (thing.Value<bool>("internal")) ? "book" : "external-link";
                <ul class="list-group" style="font-size: small">
                    <li class="list-group-item active">
                        <h3>Related links track external test</h3></li>
    
                    <li><a href="@linkUrl" target="@linkTarget">@(thing.Value<string>("caption"))</a></li>
    
                    <li class="list-group-item">
                        @if (linkType == "external-link")
                        {
                            <a href="@linkUrl" @Html.Raw(linkTarget)>@(thing.Value<string>("title"))&nbsp;<abbr class="ext" title="External website">ext</abbr></a>
                        }
                        else
                        {
                            <a href="@linkUrl" @Html.Raw(linkTarget)>@(thing.Value<string>("title"))&nbsp;</a>
                        }
                    </li>
                    }
                </ul>
            }
        }
    } 
    
  • Craig Mayers 164 posts 508 karma points
    May 18, 2017 @ 13:51
    Craig Mayers
    0

    Found it...

    Could do with being refactored, couple years old now:

    /// <summary>
        /// Gets the links.
        /// </summary>
        /// <param name="nodeId">The node identifier.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public static IEnumerable<RelatedLink> GetLinks(int nodeId, string propertyName)
        {
            if (nodeId > 0)
            {
                Node currentNode = new Node(nodeId);
    
                string links = currentNode.GetProperty(propertyName).Value;
    
                if (!string.IsNullOrEmpty(links))
                {
                    var xmlSource = XDocument.Parse(links);
    
                    IEnumerable<XElement> linksList = from x in xmlSource.Descendants("link")
                                                      select x;
    
                    var dataSource = from l in linksList
                                     select new RelatedLink
                                     {
                                         LinkTitle = l.Attribute("title").Value,
                                         Link = l.Attribute("link").Value,
                                         LinkType = l.Attribute("type").Value
                                     };
    
                    return dataSource;
                }
            }
    
            return null;
        }
    

    Hope this helps OR gives you an idea of the direction to take it.

    Good luck!

    Craig

  • kleptbit 42 posts 98 karma points
    May 18, 2017 @ 14:16
    kleptbit
    0

    Thanks Craig, thanks Alex. I have to go out now but cheers for this, I will find my way later I have no doubt!

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    May 18, 2017 @ 15:23
    Alex Skrypnyk
    0

    And please share with us, what is the solution to this issue )

    Have a nice evening!

  • kleptbit 42 posts 98 karma points
    May 18, 2017 @ 16:52
    kleptbit
    0

    And you Alex!

    Well so far I don't think I can use the JArray. I'm getting an Argument NullException:

    "Value cannot be null.\r\nParameter name: source"
    

    so the string isn't getting converted to a JArray and thus the source is null...I think. But hey, it's a new error message, so progress of a sort!

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    May 18, 2017 @ 21:13
    Alex Skrypnyk
    0

    Then you need to use code like Craig suggested, parse value manually.

  • kleptbit 42 posts 98 karma points
    May 19, 2017 @ 09:59
    kleptbit
    0

    Yes I think I do, but I am not quite sure how to do that. I have put it in the App_Code folder but it 'expects class, delegate, enum, interface or struct'.

    Do I need to make an IEnumerable

    Would you know of any links to tutorials to help me understand what to do with it please? My skills need improvement!

    Sorry for the basic questions.

Please Sign in or register to post replies

Write your reply to:

Draft