Get current shop and language in Dynamicweb Razor templates

Razor coding tip


Coding your templates in Razor, for instance using C# (.cshtml templates) opens up a room of many nice facilities. However getting started can be a bit hard as you are entering an area where you need to know your Dynamicweb API and framework.


I recently had to generate a Google Shopping Feed, and I needed the simple information about the current shopID and LanguageID. In HTML templates you rely on tags existing for your need, which makes things easy: You have the tags or you don’t!

The Dynamicweb API is rather big

In the days of HTML (and XSLT) templates you (as a front-end developer) would properly ask an inhouse backend developer or the Dynamicweb Support to please create a new tag for you. That was both easy and also a bit frustrating: You were relying on being able to convince someone else to please make the information available for you, which was not always possible.

You now can use the Dynamicweb API or the .NET framework to find the information your self. You how should always ask a backend developer to review your code, so that you do clever code, which will not do bad things to performance.

You can learn more about the Dynamicweb API by browsing Dynamicweb 8 Documentation. Also some pretty useful tips can be found on the Dynamicweb Razor github page, for instance the Recipes page.

Finding the ShopID and LanguageID

Here you are, 4 lines of C# code which will find the EcomLanguagID and EcomShopID:

var pageView = Dynamicweb.Frontend.PageView.Current() ?? Dynamicweb.Frontend.PageView.GetPageview();
var area = Dynamicweb.Content.Area.GetAreaById(pageView.AreaID);
string EcomLanguageID = area.EcomLanguageID;
string EcomShopID = area.EcomShopID;

It is not rocket science but starting to write Razor templates it still is unknown territory, which makes it almost rocket science.

Current pageview – your page scope

Your world when coding Razor templates is the Current PageView. The Pageview object lives in the Dynamicweb.Frontend namespace, it holds many of the relevant objects all playing a part of a Dynamicweb webpage. As with scopes in general, investing time in getting to know your scope, your “living room” makes a big difference. So do invest some time into the documentation for the Pageview Class.

Area object – your website scope

Using the PageView we can locate the Area object. We do that by calling the method “Dynamicweb.Content.Area.GetAreaById” with the AreaID found on the Pageview. So we gently ask Dynamicweb CMS:

“Could you please hand over the Area Object in which my current page is living?”

We will now get another “room” (the Area Object) which in practice is the “website scope” in which your page scope lives. The more “general” things can be found here.
Again, do refer to the Dynamicweb Area Class Documentation if you want to go hunting for members like Public properties, Methods and Constructors living in the Area scope.

Coding in C# in Razor without knowing the API is like living in a house which you have not taken a walk inside. If I ask you: “Where can I find a WC?” you will promptly answer it with an explanation how I can locate it. Your target as a developer should (in a perfect world) also be to know as well the API which defines your daily world doing development.

The information are on the Area Object!

Finally I found the information! The two relevant information were exposed as two Public Getters/Properties on my current Area Object:


Leave a Reply