Copied to clipboard

Flag this post as spam?

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


  • Richard van der Meel 13 posts 77 karma points
    Nov 29, 2011 @ 09:57
    Richard van der Meel
    0

    How to handle XML property in Razor, using Macro parameter for propertyalias

    Hi all,

    I'm trying to handle an xml property in razor. The propertyalias is passed as an macro parameter.

    In the example below i'm trying to get the value for an ucomponents relatedlinks datatype stored as xml.

    var linksProperty = Model.AncestorOrSelf("site").GetPropertyValue(Parameter.PropertyAlias);
    
    linksProperty contains:
    <links><link title="Example" link="1116" type="internal" 
    newwindow="0" /><link title="Example2" link="1117" 
    type="internal" newwindow="0" /></links>

    Now i like to iterate all links and be able to read all properties like: newwindow, type, etc. The cultiv examples project for razor does handle the process when knowing the propertyalias in code, but not how to handle it in a dynamic way.

    Any ideas?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Nov 29, 2011 @ 10:09
    Dirk De Grave
    0

    Richard,

    one way to handle this is to convert the output of the GetPropertyValue("") to a XDocument and use Linq2Xml to query the underlying xml fragment.

    var xml = XDocument.Parse(Model.AncestorOrSelf("site").GetPropertyValue(Parameter.PropertyAlias).ToString());
    foreach (XElement link in xml.Descendants("link")) {
    //... do whatever you need to do here..
    }

    Hope this helps.

    Regards,

    /Dirk

     

  • Richard van der Meel 13 posts 77 karma points
    Nov 29, 2011 @ 10:14
    Richard van der Meel
    0

    Hi Dirk,

    I thought about doing it that way, but isn't it possible to handle it in a typed way?
    Usually we use something like:

    @foreach (var item in Model.UcRelatedLinks)
            {
                var target = item.newwindow == "1" ? "target=\"_blank\"" : "";
                
                    @* The output is different for media, internal and external links *@
                    @{
                        string type = item.type;
                        switch (type)
                        {
                            case "internal":
                                @item.title
                                break;
                            case "external":
                                @item.title
                                break;
                        }
                    }
                
            } 

     Only problem is that i don't know UcRelatedLinks and something below does not work..

    umbraco.MacroEngines.DynamicXml linksProperty = Model.AncestorOrSelf("site").GetProperty(property);

    because the PropertyResult cannot be casted that way.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Nov 29, 2011 @ 10:18
    Dirk De Grave
    0

    I haven't tried this one yet, but could be your solution as it has support for core Related Links datatype: http://razordatatypemodels.codeplex.com/

     

    Cheers,

    /Dirk

  • Richard van der Meel 13 posts 77 karma points
    Nov 29, 2011 @ 10:22
    Richard van der Meel
    0

    Jow thanks Dirk, had not found that yet...

    Trying it now and will post the results later on...

Please Sign in or register to post replies

Write your reply to:

Draft