When you are working with content within the PIM, you may want to create multiple associations from the products and variants to different types of node. Before we get into the code, I should probably mention that an association in Episerver commerce is different to the normal Episerver CMS navigation hierarchy structure. In the CMS for example, a page can only have one parent. In Episerver Commerce things work slightly differently.
A product or variant can be associated under multiple nodes. This is really useful when you have requirements like, creating a central store for all your products and then having a discounted node section to apply discounts to products. Another potential example could be on multi-region sites where a product may need to live under several country nodes to be applicable to be sold within that market.
As Episerver Commerce works slightly differently than CMS, it also means that the API call is slightly different than the standard CMS approach.
In Episerver Commerce when we want to create one of these associations we use the ILinksRepository within the EPiServer.Commerce.Catalog.Linking namespace.
var linksRepository = ServiceLocator.Current.GetInstance<ILinksRepository>();
var variantToCreateAssociationWith = new ContentReference();
var nodeToHostTheVariantAssociation= new ContentReference();
var linkedEntry = new NodeRelation()
SortOrder = 0,
Source = variantToCreateAssociationWith ,
Target = nodeToHostTheVariantAssociation,
Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge