Copied to clipboard

Flag this post as spam?

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


  • Lex Godthelp 24 posts 106 karma points
    Jun 22, 2015 @ 11:39
    Lex Godthelp
    0

    Getting a VortoValue from a specific node

    Hello guys,

    I just started off with Razor and i am running into all kinds of trouble and i was hoping you could help me along my way. I always work with XSLT but for this project we decided to use Razor because of Vorto (specifically made for Multilingual sites).

    I'm trying to do something really simple but i can't find any documentation on how to do it. I want to make a list with childpages from a specific page. How would i go around doing this by using id instead of the current selection i'm using. I also want to use the same selection to add the value of a field inside that specific node as h2.

    @using  Our.Umbraco.Vorto.Extensions
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{var selection = CurrentPage.Site().FirstChild("Modules").Children("Module");}
    <h2></h2>
    <ul>
    @foreach(var item in selection){
    IPublishedContent sitem= (IPublishedContent)item;
        <li><a href="@item.Url">@sitem.GetVortoValue("paginatitel")</a></li>
    } 
    </ul>
    
  • René Pjengaard 117 posts 700 karma points c-trib
    Jun 22, 2015 @ 12:57
    René Pjengaard
    0

    Hi Lex,

    you can get children from a specific page by using

    IPublishContent[] children = UmbracoContext.Current.ContentCache.GetById(1234).Children("Module");
    

    You can get properties from child-node this way:

    <h2>@(Model.GetPropertyValue("propertyAlias"))</h2>
    

    Question! Why are you using the sitem var instead af using the item? You could go:

    @foreach(IPublishContent item in selection) { 
        <li><a href="@item.Url">@item.GetVortoValue("paginatitel")</a></li>
    }
    

    I haven´t tested the code, but it schould work.

    Regards René

  • Lex Godthelp 24 posts 106 karma points
    Jun 22, 2015 @ 13:12
    Lex Godthelp
    0

    Hi René,

    Thank you for your reply. Could you show me how to put this all together, because the problem i keep having when constructing Razor is that i somehow do something wrong which makes my macro fail.

    I am using the @sitem because of

    this

  • René Pjengaard 117 posts 700 karma points c-trib
    Jun 22, 2015 @ 13:53
    René Pjengaard
    100

    Hi Lex,

    i hardly use MacroPartials anymore. But i can try giving it a go. I presume you are using a macroparameter to set the id of the parent node.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var id = 0;
    
        // get id from ex. contentPicker datatype on macro
        int.TryParse(Model.MacroParameters["parentNode"].ToString(), out id);
    
        // get IPublishContent object from id
        IPublishedContent parent = UmbracoContext.Current.ContentCache.GetById(id);
    
        // get children by documenttype "Module"
        IPublishedContent[] children = parent.Children.Where(x => x.DocumentTypeAlias == "Module").ToArray();
    }
    <h2>@(parent.GetPropertyValue<string>("headline"))</h2>
    
    @if (children.Any()){
        <ul>
            @foreach (IPublishedContent item in children)
            {
                <li><a href="@item.Url">@(item.GetPropertyValue<string>("pagetitle"))</a></li>
            }        
        </ul>
    }
    

    /René

  • Lex Godthelp 24 posts 106 karma points
    Jun 22, 2015 @ 14:03
    Lex Godthelp
    0

    Hello René, i wasn't setting the id with a MacroParameter but as a hard value. So with a little bit of Googling i changed:

    int.TryParse(Model.MacroParameters["parentNode"].ToString(), out id);
    

    To:

    int.TryParse(1128.ToString(), out id);
    

    Which works perfect. Thank you so much for your help René!

  • René Pjengaard 117 posts 700 karma points c-trib
    Jun 22, 2015 @ 14:14
    René Pjengaard
    0

    Hi Lex,

    great you got it working! :-)

    Extra bonus info: you don´t need to int.TryParse when you hardcode the id. So you can do just:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage @{ // get IPublishContent object from id IPublishedContent parent = UmbracoContext.Current.ContentCache.GetById(1128);
    
    // get children by documenttype "Module"
    IPublishedContent[] children = parent.Children.Where(x => x.DocumentTypeAlias == "Module").ToArray();
    }
    

    /René

Please Sign in or register to post replies

Write your reply to:

Draft