Jonas Bergqvist
Apr 29, 2014
  6585
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024