Copied to clipboard

Flag this post as spam?

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


  • Adam Jenkin 71 posts 226 karma points
    Jan 09, 2012 @ 11:56
    Adam Jenkin
    0

    Iterating over items from DynamicXml - Related Links Data

    Hi All, 

    Im trying to use razor to iterate over a field of RelatedLinks against my homepage node. I can see the field is returned in razor as DynamicXml and therefore believe I should be able to use dot notation to access and itterate over the contents (from the razor feature walkghtoughs). When I use '.links' to jump into the first element of the xml, I receive 'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'links'. Here is my code:

     

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;
    @using System.Linq           
    @{
        // Get the hompage node
        var homepage = @Model.AncestorOrSelf("Start");
    }
    @* debugging *@
    @homepage.siteLinksFirstSet.ToXml();
    @*
    Above looks like:
      <links> <link title="membership" link="1075" type="internal" newwindow="0" /> <link title="google" link="http://google.com" type="external" newwindow="0" /> </links>; 
      
     .. So im led to believe I can do this:
    *@
    <br />
    @foreach(var l in homepage.siteLinksFirstSet.links)
    {
    @l.title
    }
    @*
     .. but im getting 'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'links' :(
    *@

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 09, 2012 @ 12:08
    Sebastiaan Janssen
    0

    It will skip the first "links" node because that is used as the root of your XML. So just do a foreach on homepage.siteLinksFirstSet and you should be good to go!

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 09, 2012 @ 12:10
    Sebastiaan Janssen
    0

    Ps. a similar example is in my Razor examples project (on the uComponents page), it adds some checks in case you leave it empty. You might need to check if there's only 1 link in there as well, not sure how that would be handled.

  • Adam Jenkin 71 posts 226 karma points
    Jan 09, 2012 @ 12:37
    Adam Jenkin
    1

    Thanks Seb! That was it!

    Taken your HasProperty and HasValue examples.

    When only 1 item is in the array - it continues to work fine :)

Please Sign in or register to post replies

Write your reply to:

Draft