The RelationService
is pretty awesome as it allows you to create relations between objects that would otherwise have no obvious connection.
Browse the API documentation for IRelationService interface.
Namespace: Umbraco.Cms.Core.Services
Assembly: Umbraco.Core.dll
Checks if two items are related.
Returns bool
.
Checks if two items are related.
Returns bool
.
Checks if two items are related.
Returns bool
.
Deletes a relation.
Returns void
.
Deletes a relation type.
Returns void
.
Deletes relation of the specified relation type.
Returns void
.
Gets a collection of Umbraco.Core.Models.Relation
objects. Optional array of integer ids to return relations for.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects by their relation type.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects by their relation type id.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects. Optional array of integer ids to return relationtypes for.
Returns IEnumerable<IRelationType>
.
Gets a collection of Umbraco.Core.Models.Relation
objects by their child entity.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects their child entity and relation type alias.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects by their child id.
Returns IEnumerable<IRelation>
.
Gets a Umbraco.Core.Models.Relation
object by its id.
Returns IRelation
.
Gets a collection of Umbraco.Core.Models.Relation
objects by their parent entity and relation type alias.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects by their parent entity.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects by their parent id.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects by their parent or child id and relation type alias.
Returns IEnumerable<IRelation>
.
Using this method will get you all relations regards of it being a child or parent relation.
Gets a collection of Umbraco.Core.Models.Relation
objects by their parent or child id.
Returns IEnumerable<IRelation>
.
Using this method will get you all relations regards of it being a child or parent relation.
Gets a collection of Umbraco.Core.Models.Relation
objects by their relation type alias.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects by the id of their relation type.
Returns IEnumerable<IRelation>
.
Gets a collection of Umbraco.Core.Models.Relation
objects by the name of their relation type.
Returns IEnumerable<IRelation>
.
Gets the child objects from a collection of IRelation
as a collection of Umbraco.Core.Models.Entities.IUmbracoEntity
.
Returns IEnumerable<IUmbracoEntity>
.
Gets the child object from a relation as an Umbraco.Core.Models.Entities.IUmbracoEntity
object.
Returns IUmbracoEntity
.
Gets the parent and child objects from a relation as a System.Tuple
with Umbraco.Core.Models.Entities.IUmbracoEntity
.
Returns Tuple<IUmbracoEntity, IUmbracoEntity>
.
Gets the parent and child objects from a collection of relations as a list of Umbraco.Core.Models.Entities.IUmbracoEntity
objects.
Returns IEnumerable<Tuple<IUmbracoEntity, IUmbracoEntity>>
.
Gets the parent objects from a collection of relations as a collection of Umbraco.Core.Models.Entities.IUmbracoEntity
.
Returns IEnumerable<IUmbracoEntity>
.
Gets the parent object from a relation as an Umbraco.Core.Models.Entities.IUmbracoEntity
object.
Gets an relation by its alias.
Returns IRelationType
.
Gets a relation type by its Id
Returns IRelationType
.
Gets a relation type by its id.
Returns IRelationType
.
Checks if any relations exist for the specified relation type.
Returns bool
.
Checks if any relations exist for the specified id.
Returns void
.
Relates two objects by their ids using the specified relation type.
Returns IRelation
.
Relates two IUmbracoEntity
objects using the specified relation type.
Returns IRelation
.
Relates two IUmbracoEntity
objects using the specified relation type alias.
Returns IRelation
.
Relates two IUmbracoEntity
objects using the specified relation type alias.
Returns IRelation
.
Saves a relation.
Returns Void
.
Saves a relation type.
Returns Void
.
Below you will examples using the RelationService
.
Odd example, I know.. but why not?
To perform the said task we need a component in which we can register to the ContentService.Published
event:
(You can read more about composing Umbraco here)
To have Umbraco recognize our component we need to register it in a composer:
If I know Save and Publish
my Products
node I get the following result:
Cool! Now let us try and fetch the data from an API.
Notice the x => new Relation()
? We need to make sure what we are returning can be serialized. Therefore the Relation
class is:
Browsing /umbraco/api/relations/getbyrelationtypealias?alias=homesick
now returns the following:
If you want to do something similar to this it is recommended that you wrap a caching layer around it, as the RelationService queries the database directly.