Applies to Umbraco 6.1.1 and newer
The RelationService acts as a "gateway" to Umbraco data for operations which are related to Relations.
Browse the API documentation for RelationService.
- Namespace:
Umbraco.Core.Services - Assembly:
Umbraco.Core.dll
All samples in this document will require references to the following dll:
- Umbraco.Core.dll
All samples in this document will require the following usings:
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
The RelationService is available through the ApplicationContext, or if you are using a SurfaceController or the UmbracoUserControl then the RelationService is available through a local Services property.
Services.RelationService
Getting the service through the ApplicationContext:
ApplicationContext.Current.Services.RelationService
Gets a Relation object by its Id
Gets a RelationType object by its Id
Gets a RelationType object by its Alias
Gets an enumerable list of Relation objects. If the optional array of integer ids is passed in then only the Relation objects with matching Id's will be returned.
Gets an enumerable list of Relation objects which have the specified RelationType.
Gets an enumerable list of Relation objects which have the specified RelationType Id.
Gets an enumerable list of RelationType objects. If the optional array of integer ids is passed in then only the RelationType objects with a matching Id will be returned.
Gets an enumerable list of Relation objects that have the specified ParentId.
public IENumerable<IPublishedContent> GetFavorites(int memberId)
{
var rs = ApplicationContext.Current.Services.RelationService;
var relType = rs.GetRelationTypeByAlias("memberFavorites");
var favorites = new List<IPublishedContent>();
if (memberId > 0)
{
var relations = rs.GetByParentId(memberId).Where(r => r.RelationType.Alias == "memberFavorites");
foreach (var relation in relations)
{
favorites.Add(UmbracoContext.Current.ContentCache.GetById(relation.ChildId));
}
}
return favorites;
}
Gets an enumerable list of Relation objects that have the specified ChildId.
Gets an enumerable list of Relation objects that have the specified ParentId or ChildId.
Gets an enumerable list of Relation objects that have the specified RelationType Name.
Gets an enumerable list of Relation objects that have the specified RelationType Alias.
Gets an enumerable list of Relation objects that have the specified RelationType Id.
Gets the Child object from a Relation passed into the method. If the loadBaseType parameter is true the complete object graph will be loaded.
Gets the Parent object from a Relation passed into the method. If the loadBaseType parameter is true the complete object graph will be loaded.
Gets the Parent and Child objects from a Relation passed into the method. If the loadBaseType parameter is true the complete object graph will be loaded.
Gets the Child objects from a list of Relations passed into the method. If the loadBaseType parameter is true the complete object graph will be loaded.
Gets the Parent objects from a list of Relations passed into the method. If the loadBaseType parameter is true the complete object graph will be loaded.
Gets the Parent and Child objects from a list of Relations passed into the method. If the loadBaseType parameter is true the complete object graph will be loaded.
Relates two objects that implement the IUmbracoEntity interface using the RelationType matching the specified RelationType and returns the resulting Relation object.
Relates two objects that implement the IUmbracoEntity interface using the RelationType matching the specified relationTypeAlias and returns the resulting Relation object.
Returns true if there are any Relation objects with the specified RelationType, otherwise returns false.
Returns true if any relations exist for the specified Id, otherwise returns false.
Saves a single Relation object.
public void SetFavorite(int memberId, int contentId) {
var rs = ApplicationContext.Current.Services.RelationService;
var areRelated = rs.AreRelated(memberId, contentId, "memberFavorites");
if (!areRelated)
{
//create relation
var relType = rs.GetRelationTypeByAlias("memberFavorites");
var r = new Relation(memberId, contentId, relType);
rs.Save(r);
}
}
Saves a single IRelationType object.
Permanently deletes a Relation object.
Permanently deletes a RelationType object.
Permanently deletes all relations that match the specified RelationType.