Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 232 posts 902 karma points c-trib
    May 14, 2023 @ 09:19
    Martin Rud
    0

    How to get related pages in a partial or template (v11)?

    Hi,

    When editors have added relations between pages, how can I get them in a partial or template?

    I can't find a solution or link to one here: https://docs.umbraco.com/umbraco-cms/fundamentals/data/relations/

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    May 14, 2023 @ 19:22
    Marc Goodson
    0

    Hi Martin

    You'd need to use the RelationService

    There is a list of methods here:

    https://docs.umbraco.com/umbraco-cms/reference/management/services/relationservice

    You can inject the RelationService into a partial or template by putting

    @inject IRelationService RelationService
    

    at the top, and then you can get all the relations for a particular relation type for the current page with

    @foreach (var relation in RelationService.GetByParentOrChildId(Model.Id, "someRelationTypeAlias"){
    <li>@relation.ParentId - @relation.ChildId - @relation.Comment</li>
    }
    

    (or use those Umbraco Cache to retrieve the IPublishedContent for those ChildId/ParentId)

    The key thing to realise when using relations is it operates directly onto the database - and so can be super slow - usually you would make sure calls to find related items are 'cached' with your own caching mechanism, or the View is output cached inside the Cache Tag Helper (There is a Cache Tag Helper specifically for Umbraco in this package - https://github.com/umbraco-community/Our-Umbraco-TagHelpers)

    regards

    Marc

  • Martin Rud 232 posts 902 karma points c-trib
    May 15, 2023 @ 06:23
    Martin Rud
    0

    Thanks. Looks like something I can work with.

    However; I get this error:

    The type or namespace name 'IRelationService' could not be found (are you missing a using directive or an assembly reference?) [project-name]csharp(CS0246)
    

    enter image description here

  • Bo Jacobsen 593 posts 2389 karma points
    May 15, 2023 @ 06:54
    Bo Jacobsen
    1
    @using Umbraco.Cms.Core.Services
    @inject IRelationService RelationService
    
    or 
    
    @inject Umbraco.Cms.Core.Services.IRelationService RelationService
    
  • Martin Rud 232 posts 902 karma points c-trib
    May 15, 2023 @ 08:51
    Martin Rud
    0

    Cool, thanks!

    Now I am stuck with declaring RelationService:

    var RelationService = ...
    foreach (var relation in RelationService.GetByParentOrChildId(Model.Id, "SiteCases"){
        @relation.ParentId - @relation.ChildId - @relation.Comment
    }
    

    What should I write instead of the dots?

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    May 15, 2023 @ 09:54
    Marc Goodson
    100

    Hi Martin

    Once you've @injected it... you don't need to declare a new variable to use it so

    // don't need to do this..... var RelationService = ...
       // you can just refer to it as RelationService in the foreach:
        foreach (var relation in RelationService.GetByParentOrChildId(Model.Id, "SiteCases"){
            @relation.ParentId - @relation.ChildId - @relation.Comment
        }
    

    regards

    Marc

  • Martin Rud 232 posts 902 karma points c-trib
    May 15, 2023 @ 10:34
    Martin Rud
    1

    Thanks! I now see that the error I had came from a missing closing parenthesis:

    foreach (var relation in RelationService.GetByParentOrChildId(Model.Id, "SiteCases") {
    

    should be:

    foreach (var relation in RelationService.GetByParentOrChildId(Model.Id, "SiteCases")) {
    

    :)

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    May 15, 2023 @ 11:05
    Marc Goodson
    0

    hurrah!

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    May 15, 2023 @ 07:59
    Marc Goodson
    0

    Yes, Bo is right, I missed off the namespace!

    If you find you are writing lots of Using and Inject statements at the top of your views and partial views then, you can add them in one place inside a special razor file called

    /views/_ViewImports.cshtml

    and then RelationsService will be ready to use in any view or partial view without having to repeat adding the Using statement at the top of each file, but if it's only being used in one partial then adding it to the top of just that one can make sense too!

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft