HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Categorizations

Describes how to work with categorizations in relation to the Optimizely Commerce (PaaS) content model.

Categorizations 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 categorizations.

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

[New in 11.2.0]

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 categorization for the entry.

While each entry can have multiple categorizations 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 categorizations of an entry or category

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

Sample code

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

Adding a categorization to an entry or category

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

Sample code

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);
      }

Removing a categorization from an entry or category

To remove a categorization, call the RemoveRelations method or RemoveRelation extension method of IRelationRepository with a NodeRelation object matching an existing categorization. You can either 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.

Sample code

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);
      }