(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)
The type or namespace name 'IRelationService' could not be found (are you missing a using directive or an assembly reference?) [project-name]csharp(CS0246)
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
}
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!
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/
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
at the top, and then you can get all the relations for a particular relation type for the current page with
(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
Thanks. Looks like something I can work with.
However; I get this error:
Cool, thanks!
Now I am stuck with declaring RelationService:
What should I write instead of the dots?
Hi Martin
Once you've @injected it... you don't need to declare a new variable to use it so
regards
Marc
Thanks! I now see that the error I had came from a missing closing parenthesis:
should be:
:)
hurrah!
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
is working on a reply...