API

The Dynamicweb API makes it possible to interact directly with various object types in Dynamicweb outside of the TemplateTag and ViewModel systems.

In general you should only use the API if you:

  • Cannot accomplish what you want using normal means, i.e. via TemplateTags & ViewModels
  • Know what you’re doing and that understand you can really screw your performance if you’re not careful
  • Accept full responsibility for your own code

A typical usecase could be that you need to work with something for which there is no TemplateTag – like displaying the loyalty point balance of the current user in the shopping cart – or need to access a property on the product object which hasn’t been added to the ViewModel (like the product type).

To use the API you must usually go through one of the following classes:

Under each of these you will have access to other services - like the IPageService or the IAreaService - and/or system object (e.g. Dynamicweb.Security.UserManagement.User) and methods related to them (e.g. GetCurrentExtranetUser

Since this is a website and not an IDE with code-hinting, any example I give here will be less than useful since you won’t be able to see important information about e.g. the parameters passed to the API, but still – here are a couple of examples:

RAZOR
@{ <!--Product object--> @if (Model.Products != null) { foreach (var product in Model.Products) { var thisproduct = Dynamicweb.Ecommerce.Services.Products.GetProductById(product.Id, product.VariantId, product.LanguageId, false); } } <!--Check if a Back-in-Stock notification exists for thisproduct--> bool BackInStockNotification = Dynamicweb.Ecommerce.Products.ProductBackInStockNotification.BackInStockNotificationExists(thisproduct, thisproduct.DefaultUnitId); <!--Check if current user is logged in--> var isLoggedIn = Dynamicweb.Security.UserManagement.User.IsExtranetUserLoggedIn(); <!--Get currently logged in user--> var currentUser = Dynamicweb.Security.UserManagement.User.GetCurrentExtranetUser(); <!--Get loyalty points balance for current user--> @if (isLoggedIn) { double pointsbalance = Dynamicweb.Ecommerce.Services.Loyalty.GetPointsBalance(currentUser.ID); } }