Jonas Bergqvist
Apr 29, 2014
  5681
(3 votes)

How to register the partial routing in Commerce

To make a site work in Commerce 7.5, we have to register a partial router that will handle the catalog routing. The partial router, that comes out of the box, is called ‘hierarical catalog partial router’. This partial router needs to be registered during initialization if it’s intended to be used.

Different ways of register the partial route in an initialization module

We should always use an initialization module for the registration of the partial route. If there is no initialization module in the project that fits this purpose, create one.

An initialization module is a class that implements the interface “IInitializableModule”. When used in commerce, the class should also be decorated with the “ModuleDependency” attribute, with it’s type set to Commerce.Initialization.InitializationModule.

[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
public class Initialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
    }
}

PartialRouteHandler

To register the partial router, we can use the “PartialRouteHandler” inside the “Initialize” method of our initialization module.

We will first get the services “ReferenceConverter”, “IContentLoader”, and “PartialRouteHandler”. Those will be used for getting the catalog root content, and for the registration.

We will get the catalog root link using the reference converter. When we have the content link, it’s easy to get the content using the content loader.

At last we will create a hierarchical catalog partial router. We will in this example set the start page for the route to the start page of the route. This could be changed to be a setting property on the start page type. We will also tell the partial router that it should not use SEO Uri:s when creating links. By changing false to true for the last parameter, SEO Uris would be generated as links for content on the site.

var referenceConverter = context.Locate.Advanced.GetInstance<Mediachase.Commerce.Catalog.ReferenceConverter>();
var contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();
var partialRouteHandler = context.Locate.Advanced.GetInstance<PartialRouteHandler>();

var commerceRootContent = contentLoader.Get<Commerce.Catalog.ContentTypes.CatalogContentBase>(referenceConverter.GetRootLink());
var hierarchicalCatalogPartialRouter = new HierarchicalCatalogPartialRouter(() => SiteDefinition.Current.StartPage, commerceRootContent, false);

partialRouteHandler.RegisterPartialRouter(new PartialRouter<Core.PageData, Commerce.Catalog.ContentTypes.CatalogContentBase>(hierarchicalCatalogPartialRouter));

RegisterPartialRouter extension method

There is an extension method in the namespace EPiServer.Web.Routing, that makes the registration a little bit easier. By using the method “RegisterPartialRouter”, we don’t have to use the partial route handler directly.

var referenceConverter = context.Locate.Advanced.GetInstance<Mediachase.Commerce.Catalog.ReferenceConverter>();
var contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();

var commerceRootContent = contentLoader.Get<Commerce.Catalog.ContentTypes.CatalogContentBase>(referenceConverter.GetRootLink());
var hierarchicalCatalogPartialRouter = new HierarchicalCatalogPartialRouter(() => SiteDefinition.Current.StartPage, commerceRootContent, false);

RouteTable.Routes.RegisterPartialRouter(hierarchicalCatalogPartialRouter);

MapDefaultHierarchialRouter extension method

In cases where the the catalog content should be set to the catalog root (as in the examples above), another extension method can be used, which is located in the namespace EPiServer.Commerce.Routing. The extension method is called “MapDefaultHierarchialRouter”, and it’s only possible to set the start page, and SEO Uri flag on that one.

CatalogRouteHelper.MapDefaultHierarchialRouter(RouteTable.Routes, () => SiteDefinition.Current.StartPage, false);

There is also an overload that only set the SEO Uri flag. This one will use the SiteDefinition.Current.StartPage as the start page for the partial route.

CatalogRouteHelper.MapDefaultHierarchialRouter(RouteTable.Routes, false);

Using a node as the catalog root for routing

You might want to change the catalog root for the partial routing. In our example site, we have a catalog called “Departmental Catalog”, with a “Departments” node under it. We could change the registration to have Department as the root content by the following code:

[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
public class Initialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var referenceConverter = context.Locate.Advanced.GetInstance<Mediachase.Commerce.Catalog.ReferenceConverter>();
        var contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();

        var departmentCatalog = contentLoader.GetChildren<Commerce.Catalog.ContentTypes.CatalogContent>(referenceConverter.GetRootLink()).First();
        var departmentsNode = contentLoader.GetChildren<Commerce.Catalog.ContentTypes.NodeContent>(departmentCatalog.ContentLink).First();

        var hierarchicalCatalogPartialRouter = new HierarchicalCatalogPartialRouter(() => SiteDefinition.Current.StartPage, departmentsNode, false);

        RouteTable.Routes.RegisterPartialRouter(hierarchicalCatalogPartialRouter);
    }
}
Apr 29, 2014

Comments

Frederik Vig
Frederik Vig Apr 29, 2014 09:32 AM

Don't you need to pass in RootContent for HierarchicalCatalogPartialRouter?

Apr 29, 2014 12:47 PM

Frederik Vig: You had to do that before 7.7.2.74, which was released today. Get the latest package and this will work. It was a bug before that made you send in RootContent. This has been changed to CatalogContentBase.

K Khan
K Khan Apr 30, 2014 03:01 PM

What changes we will need if we will need Filters in URLs? e.g. url/color/black/size/uk10

May 1, 2014 09:13 AM

Khan: Intresting. I would probobly inherit from the HierarchicalCatalogPartialRouter and override some of the methods. This is something I will try out when I got time.

K Khan
K Khan May 8, 2014 04:37 PM

HI Jonas,

I have following urls
url/US/productseokey
url/CA/productseokey
both are pointing to same product.

But get the following error
Exception: Exception in ecf_CatalogNode_UriLanguage: Column 'CatalogNodeId' is constrained to be unique. Value '1' is already present.Column 'CatalogNodeId' is constrained to be unique. Value '1' is already present.Column 'CatalogNodeId' is constrained to be unique. Value '1' is already present.]

I can save same SEOURI key for different languages through caommerce manager and CMS/Edit window

I am wondering, do you have any idea, how can we overcome this limit

Regards
Khurram

K Khan
K Khan Aug 11, 2014 11:16 AM

Issues above have been fixed we can expect those in next in 7.10.1+
http://world.episerver.com/Forum/Developer-forum/EPiServer-Commerce/Thread-Container/2014/7/Routing-for-multilingual-site/

Please login to comment.
Latest blogs
Display Child Pages in Content Delivery API Response

The below example will implement an instance of IContentConverterProvider to customise the serialisation of PageData and output child pages in the...

Minesh Shah (Netcel) | Oct 4, 2022

Bring the Report Center back in Optimizely CMS 12

The Report Center has been a part of Optimizely CMS since its first debut in version 5R2 in 2008, but in CMS 12, it's removed! Don't despair! Make...

Tomas Hensrud Gulla | Oct 4, 2022 | Syndicated blog

Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog

Optimizely names Luminary Senior Developer, Ynze Nunnink, OMVP

Luminary Senior Developer and Optimizely Lead, Ynze Nunnink has secured the coveted position of Optimizely MVP. Earning a Platinum badge for his...

Ynze | Oct 2, 2022 | Syndicated blog

Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog

A multi-brand strategy in Optimizely DXP

The Optimizely Digital Experience Platform (DXP) is ideally suited to digital projects featuring multiple websites or applications. Here, we showca...

Ynze | Sep 29, 2022 | Syndicated blog