Copied to clipboard

Flag this post as spam?

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


  • Thomas Michael Koch 24 posts 96 karma points
    Jul 22, 2011 @ 13:41
    Thomas Michael Koch
    0

    How to get first link from RelatedLinks datatype

    Hi - I have a document type with a property of RelatedLinks.

    I would like to get the first link entered - if its there - using a Razor macro. I tried to use the Linq-like synax like: FirstOrDefault og ElementAt(0). But apparently they do not work with the list of linked lists.

    I have managed to "hack" my way through but I would like to know if there is a more elegant way of achieving the same effect as FirstOrDefault og ElementAt(0).

    Here is my hack:

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @foreach (var item in @Model.NodeById(1520).Children.Where("archived != true").OrderBy("CreatedDate desc"){

      string href "#";
      string target "";

      foreach (var link in @item.NewsUrl{
         target link.newwindow == "1" " target=\"_blank\"" "";
         if (link.type=="internal"
           href=umbraco.library.NiceUrl(int.Parse(link.link));
         
         else {
           href=link.link;
         }
         break;                               
      }

       <href="@href">
       <div class="newsBoxHeader">
         <i>@item.CreatedDate.ToString("MMMM d, yyyy")</i>
         <b>@item.Title</b>
       </div>
       <div class="newsBoxItem">
         @item.Description
       </div>
      </a>
      <div class="block10">
       <img src="/images/site/divider.png" alt="divider" />
      </div>
    }

    The thing that annoys me is the second inner foreach loop, to get the first item in the RelatedLinks field (NewsUrl).

    Regards

    Thomas


  • Thomas Michael Koch 24 posts 96 karma points
    Jul 22, 2011 @ 13:44
    Thomas Michael Koch
    0

    Forgot to mention that I am running Umbraco 4.7 and .NET4.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 22, 2011 @ 14:42
    Sebastiaan Janssen
    0

    Unfortunately, the Count() method is not implemtented in 4.7. If you grab just the umbraco.MacroEngines.dll from one of the 4.7.1 nightlies, you can do it as simple as this:

    @if(Model.relLinks.Count() > 0)
    {
        @Model.relLinks[0].title
    }
  • Thomas Michael Koch 24 posts 96 karma points
    Jul 22, 2011 @ 15:52
    Thomas Michael Koch
    0

    Hi Sebastian - thanks for the reply. :)

    I tried to use the MacroEngines.dll from the 4.7.1.408 build. However, that introduced a problem with it not being able to understand "ToString" on a date type:

    Error loading Razor Script FrontpageNews.cshtml
    The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments

    Any suggestions?

    Thomas

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 22, 2011 @ 16:15
    Sebastiaan Janssen
    0

    Ehm, not really, I'd need to see some of your code to know what's going on.

  • Thomas Michael Koch 24 posts 96 karma points
    Aug 01, 2011 @ 22:29
    Thomas Michael Koch
    0

    Hi Sebastiaan - sorry for not answering before now.

    I have decided to stick with the original solution and accept that it may not be so pretty. It works. :)

    I will keep your suggestion in mind.

    Thank you for taking the time to answer my post.

    Kind regards

    Thomas

Please Sign in or register to post replies

Write your reply to:

Draft