Jonas Bergqvist
Apr 29, 2014
  6648
(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
Optimizely Forms: You cannot submit this form because an administrator has turned off data storage.

Do not let this error message scare you, the solution is quite simple!

Tomas Hensrud Gulla | Oct 4, 2024 | Syndicated blog

Add your own tools to the Optimizely CMS 12 admin menu

The menus in Optimizely CMS can be extended using a MenuProvider, and using the path parameter you decide what menu you want to add additional menu...

Tomas Hensrud Gulla | Oct 3, 2024 | Syndicated blog

Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024