Relations
What are relations, how to create and manage them
Umbraco sections are built around the concept of 'trees' and there is an implicit relationship between items in a section tree.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-1ace04fdb615474f953cca3f8ff2ce24f29bd4d8%252Fparent-siblings-children.png%3Falt%3Dmedia%26token%3D2087ca6f-02d3-4ac3-9939-c52f81e47b32&width=768&dpr=4&quality=100&sign=2115bd2&sv=1)
We refer to these relationships in the manner of a 'Family Tree' - eg, One content item might be the 'Parent' of some content items, and those content items would be referred to as the 'Children' of that parent. Items within the same branch of the tree can also be described as 'Ancestors' or 'Descendants' of an item, depending if they are above or below them in the depth of the tree.
There are methods in Umbraco to support querying content items by their relative position in the tree to the current page using these concepts, eg Model.Ancestors()
or Model.Children()
or Model.Descendants()
- but what about 'Cousins'?
What if there are no direct Parent/Child/Ancestor/Descendant relationships between two items in a tree, but they are still somehow 'related' in the context of your website? For example, the alternate language translation pages of a content page.
What if there is also a 'relation' between different types of Umbraco entities in your site, eg Content -> Member or Member -> MediaFolder? Perhaps, you'd like to be able to retrieve and display the uploaded images from a specific logged-in Member.
These are the scenarios where the concept of Umbraco Relations provides a solution.
The Concept of Umbraco Relations
Umbraco Relations allow you to relate almost any object in Umbraco to almost any other Umbraco object - under a defined Relation Type.
How is this different to pickers?
With a Content/Member/Media picker that you might add to a Content Type - the relationship is ONLY 1-way. Your content item knows it has 'picked' another content item, perhaps to display a shared banner across different pages, but that 'banner' content item doesn't know where it has been picked!
Umbraco Relations are 2-way, when you create a relation between two different types of entities, you can alway find one entity from the other and vice versa. eg you could list out in the backoffice all the pages that a content banner had been picked on.
Relation Types
To create and use Relations in your Umbraco Website, you need to define a 'Relation Type' to specify what the two types of entities will be relatable and to give the Relation Type an alias. So, you can test if two items are related for a particular 'relation type'. Two items might be related under multiple different Relation Types, and you might be only interested in your 'Related Language Page' Relation Type.
Create a Relation Type
Navigate to the Settings section in the Umbraco backoffice and you'll find the Relation Types folder in the Settings tree.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-3c84d2a9c88fe2e0f1400b1150f455ba4b0af8d9%252Frelation-types-tree.png%3Falt%3Dmedia%26token%3De0eca3e8-2dbd-4a07-89e9-ff12039d15ec&width=768&dpr=4&quality=100&sign=9b892691&sv=1)
If you expand the Relation Type tree, you can see that Umbraco 'ships' with some default Relation Types. These are used by the Umbraco backoffice to help deliver some key backoffice functionality:
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-b27b41a342306eb5f9724d2fc5dcef26684d5808%252Fdefault-relation-types.png%3Falt%3Dmedia%26token%3Df14aee50-02af-4dde-95fe-f5f950f60bb3&width=768&dpr=4&quality=100&sign=988ad638&sv=1)
For example, there is a Relation Type that tracks when Media is picked in Content to be able to provide the functionality of warning an editor if they try to delete a Media Item that it is 'in use'. There is a Relation Type, to help 'restore' deleted content back to the place it was deleted from in the Recycle Bin.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-c41346ce89c5383dc51b5a8de076065edd9b925a%252Fcreate-relation-type.png%3Falt%3Dmedia%26token%3D356eba63-d18a-4f76-8d25-2b0fa49221ca&width=768&dpr=4&quality=100&sign=8c65925d&sv=1)
To create your new Relation Type, follow these steps:
Right-click the Relation Types folder.
Provide a Name for the Relation Type. This will generate it's alias.
Choose the Direction of the relationship, for example, usually Bidirectional to get the benefits of relations.
Define the type of one object in the relation called Parent that defines which column in the database this value is stored in.
For a bidirectional relationship, it doesn't matter which type of entity is defined as the Parent or the Child. If there is 'one thing' that will be related to lots of 'other things', then choose that thing as the Parent.
Choose the different types for each entity (Parent and child) from the drop-down list.
Currently the available types are: Document(Content), Media, Member, Document Type, Media Type, Member Type, Member Group, Data Type, Root, and Recycle Bin
Example: For relating Members to their uploaded Images, we might create a 'Member Images' relation
Member Images Select Is Dependency as Yes if you wish this Relation Type to be used in Umbraco's 'tracking' functionality; that warns editors when trying to delete entities if they are 'used' in a Relation Type marked as 'Is Dependency'
Click Create and you'll see your new Relation Type created in the Relation Types folder. You can see the 'Alias' that you'll need to make note of when working with Relations.
Member Images
Viewing Relations
To view one of the existing Relation Types, go to the Relations tab. It displays a long list of all the objects that have been related for this specific Relation Type.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-5dcd9ce63a05784c9703ea221f73f8bd89921276%252Fview-relations.png%3Falt%3Dmedia%26token%3D328b919a-a11a-4194-875e-6606173a7355&width=768&dpr=4&quality=100&sign=fa5c3345&sv=1)
Creating Relations
You can create Relations using the RelationService API via code.
Some examples are provided here in the RelationService Documentation Page
Use cases
You might want to create a 'Relation' between two objects either as:
A response to a backoffice event. For example, a content item being published that has picked several other content items. Storing a relationship between these items would make querying between them easier. Perhaps show all the pages on which a particular 'banner' has been picked.
A logged-in member on the front end of an Umbraco website might have the facility to upload images. In response, the implementation could store the photos programmatically in the Media Section and at the same time, create a Relation to record the relationship between the member and their uploaded pictures. On an image gallery page, it would be possible to display all the gallery images for the current logged-in Member using the relations.
Community Packages
Some of the community packages that use Relations are listed below:
'Relations Picker' - a content picker that automatically creates Relations.
'ContentRelations' - allows you to relate two items via the Backoffice.
'LinkedPages' - Provides a LinkedPages context item to show, edit, and add relations between content pages.
Last updated