HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Categorizations

Describes how to work with categorizations in Optimizely Commerce Connect 13.

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 property together with its Child property (referencing the item being categorized). That is, you cannot add the same categorization more than once to an entry or another category.

Commerce Connect 11.2.0

For categorizations of entries, you can choose to work with the NodeEntryRelation type instead of the NodeRelation type. NodeEntryRelation derives from NodeRelation and exposes the additional IsPrimary property, describing if the relation is the primary categorization for the entry or not.

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