Copied to clipboard

Flag this post as spam?

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


  • Thomas 66 posts 88 karma points
    Jan 27, 2012 @ 15:06
    Thomas
    0

    Using relations with razor

    How do I get started using relations in Razor?

    I have installed the Multipicker Relations, uComponents packages, and looked at various pages on relations (Hendys Blog and Umbraco TV), but not yet seen any examples in Razor.

    I am trying to establish and fetch a bidirectional relationship between members and documents.

    I know it would be easier doing this in .NET but would like to hear if any of you have experience with or examples of such relations in Razor.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 27, 2012 @ 17:08
    Jeroen Breuer
    0

    Well in Razor you can also use C# so you could do something like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;
    @using umbraco;
    
    @{
        Relation[] relations = Relation.GetRelations(document.Id, relationType);
        Member member;
        foreach (Relation relation in relations)
        {
    member = new Member(relation.Child.Id); <p>@member.Text</p> } }

    You'll need to include some more namespaces, but you get the picture :).

    Jeroen

  • Thomas 66 posts 88 karma points
    Jan 28, 2012 @ 16:53
    Thomas
    1

    Thanks a lot - I'm still new to Razor so that helped a lot :o)

    I ended up with this working code;

      @using umbraco.cms.businesslogic.relation;
      @using umbraco.cms.businesslogic.member;

      @{
        RelationType relationType = RelationType.GetByAlias("myRelationType");
        Relation[] relations = Relation.GetRelations(Model.Id, relationType);
        Member member;
      
      <h2>Relations</h2>
        foreach (Relation relation in relations)
        {
            member = new Member(relation.Child.Id);
            <p>@member.Text</p>
        }
      }


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 29, 2012 @ 11:25
    Jeroen Breuer
    0

    Glad you got it working. If you want to access member information maybe you should have a look at this topic: http://our.umbraco.org/forum/developers/extending-umbraco/27626-Accessing-Member-Information

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft