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:

  • Dynamicweb.Services is used to work with non-Ecommerce-related content such as Pages, Items, Paragraphs, etc.
  • Dynamicweb.Ecommerce.Services is used to work with Ecommerce-related content such as Products, Orders, Shops, etc.
  • Dynamicweb.Security.UserManagement is used to work with users, user groups, addresses, etc.

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); } }