Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Categories (dev)

Describes how to work with categories in relation to the Optimizely Commerce Connect content model.

Categories are represented by the EPiServer.Commerce.Catalog.Linking.NodeRelation class and administered using the EPiServer.Commerce.Catalog.Linking.IRelationRepository service.

The Parent property of the NodeRelation contains the ContentReference of the category. You can categorize each entry and category. The class also has a SortOrder property that describes the order of the categories.

A NodeRelation is uniquely defined by the ContentReference in its Parent and Child property (referencing the categorized item). You cannot add the same category more than once to an entry or another category.

For categorizations of entries, you can work with the NodeEntryRelation type instead of the NodeRelation type. NodeEntryRelation derives from NodeRelation and exposes the additional IsPrimary property, describing whether the relation is the primary category for the entry.

While each entry can have multiple categories linking it to several categories, only one can be the primary relation. The primary relation specifies the entry's primary or home category. This category is used when building the entry's default hierarchical URL.

Get the categories of an entry or category

To get categories, call the GetChildren method of IRelationRepository with the ContentReference of an entry or category.

public IEnumerable<NodeRelation> ListCategories(ContentReference referenceToEntryOrCategory)
{
  var relationRepository = ServiceLocator.Current.GetInstance<IRelationRepository>();
  var categories = relationRepository.GetChildren<NodeRelation>(referenceToEntryOrCategory);
  return categories;
}

Add a category to an entry or another category

To add NodeRelation objects to an entry or category, use the UpdateRelations method or UpdateRelation extension method of IRelationRepository. The new category must have a Parent ContentReference and a Child ContentReference.

public void AddCategory(ContentReference referenceToEntryOrCategory, ContentReference referenceToCategory)
{
  var relationRepository = ServiceLocator.Current.GetInstance<IRelationRepository>();
  var newCategory = new NodeRelation
    {
      SortOrder = 100,
      Child = referenceToEntryOrCategory,
      Parent = referenceToCategory
    };
  relationRepository.UpdateRelation(newCategory);
}

Remove a category from an entry or another category

To remove a category, call the RemoveRelations method or RemoveRelation extension method of IRelationRepository with a NodeRelation object matching an existing category. You can construct a matching object, or use GetChildren or GetParents to get the existing Relations, filter out the object you want to remove, and pass it to RemoveRelation.

public void RemoveCategory(ContentReference referenceToEntryOrCategory, ContentReference referenceToCategory)
{
  var relationRepository = ServiceLocator.Current.GetInstance<IRelationRepository>();
  // Define a relation matching the one to remove, or use
  // GetRelations to find the one you want to remove and pass that to
  // RemoveRelation
  var relationToRemove = new NodeRelation
    {
      Child = referenceToEntryOrCategory,
      Parent = referenceToCategory
    };
  // Removes matching NodeRelation, or no action if no match exists
  relationRepository.RemoveRelation(relationToRemove);
}