Copied to clipboard

Flag this post as spam?

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


  • paul 5 posts 85 karma points
    Nov 19, 2015 @ 15:04
    paul
    0

    How to use relation in Partial Views

    Hello,

    I don't find a response to my question into the doc or on the forum, but maybe i look wrong .

    So this is my structure :

    Homepage

    • About us
    • Offices
    • ...

    I copy this structure with option "relate to original" for have a relation between page in different language. The structure is now like this :

    Homepage

    • About us
    • Offices
    • ...

    Accueil

    • A propos de nous
    • Bureaux
    • ....

    So this is my question, how to make in partial view a link (href) for go to the relate page.

    for example when i'm on the page about-us a link to page a-propos-de-nous ? I don't find how to make that.

    An idea ?

    Thanks in advance !

    Paul

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Nov 19, 2015 @ 22:30
    Marc Goodson
    101

    Hi Paul

    You can use the RelationService to query relations setup on your site:

    https://our.umbraco.org/documentation/reference/management/services/relationservice

    The relate on copy will create entries in the Relations Section of your site under the alias 'relateDocumentOnCopy'

    The original page id will be stored as the 'parent' of the relation and the copy page id will be stored as the 'child' in the relationship.

    So for example in your partial view:

        @{
    // get a reference to the relation service
        var rs = ApplicationContext.Current.Services.RelationService;
        // get a list of all relations caused by copying from the current node
            var copies = rs.GetByParentId(Model.Content.Id).Where(f => f.RelationType.Alias == "relateDocumentOnCopy").ToList();
    // if this is the copied page see, if it is related to an original page
            var originals = rs.GetByChildId(Model.Content.Id).Where(f => f.RelationType.Alias == "relateDocumentOnCopy").ToList();
    
     //there should only be one original
            var originalRelation = originals.FirstOrDefault();
            }
            @if (copies.Any())
            {
                <h2>Copies</h2>
                foreach (var copyRelation in copies)
                 {
    // write out each node copied from this original
                         var copiedNode = Umbraco.TypedContent(copyRelation.ChildId);
                         if (copiedNode != null)
                         {
    `// (you could check copiedNode.Path to see if it is located in the site you` want to link to)
                             <a href="@copiedNode.Url">@copiedNode.Name</a>
                         }
                     }
            }
    
           @if (originalRelation != null)
            {
    // write out the original node this page was copied from
                var originalNode = Umbraco.TypedContent(originalRelation.ParentId);
                <h2>Original</h2>
             <a href="@originalNode.Url">@originalNode.Name</a>
            }
    

    if that helps get you started ?

    regards

    Marc

  • paul 5 posts 85 karma points
    Nov 20, 2015 @ 13:47
    paul
    0

    Hey Marc !

    Thank you so much, it helps me to continue.

    I do not quite understand documentation

    Thanks again

Please Sign in or register to post replies

Write your reply to:

Draft