Developer forum

Forum » Development » DW 9.3 Creating links for products/groups pulled from index via the api

DW 9.3 Creating links for products/groups pulled from index via the api

Evaldas Raisutis
Evaldas Raisutis
Reply

I had to combine several search results from the index, so I figured performance wise it should be best if I ran searches directly against the api. I built a webApi controller to for interaction between front-end and backend - results are returned as json and rendered by javascript.

However, I can't figure out how I should generate links for products/groups I retrieve from the index. If I look at product-list-json.html template which generates JSON for an Ecom module, it has something like this:

[<!--@LoopStart(Products)--><!--@If(Products.LoopCounter>1)-->,<!--@EndIf-->{ "id": "<!--@Ecom:Product.ID.JSEncoded()-->", "name": "<!--@Ecom:Product.Name.JSEncoded()-->", "number": "<!--@Ecom:Product.Number.JSEncoded()-->", "price": "<!--@Ecom:Product.Price.JSEncoded()-->", "link": "Default.aspx?ID=<!--@Ecom:Product.PrimaryOrCurrentPageID-->&GroupID=<!--@Ecom:Product.PrimaryOrFirstGroupID-->&ProductID=<!--@Ecom:Product.ID--><!--@If Defined(Ecom:Product.VariantID)-->&VariantID=<!--@Ecom:Product.VariantID--><!--@EndIf-->", "variantid": "<!--@Ecom:Product.VariantID.JSEncoded()-->", "description": "<!--@Ecom:Product.ShortDescription.JSEncoded()-->", "image": "<!--@Ecom:Product.ImageSmall.Default.Clean.JSEncoded()-->", "synonym": "<!--@Ecom:Product:Field.Synonym.JSEncoded()-->" }<!--@LoopEnd(Products)--> ]

What I am trying to mimic is ""Default.aspx?ID=<!--@Ecom:Product.PrimaryOrCurrentPageID-->&GroupID=<!--@Ecom:Product.PrimaryOrFirstGroupID-->&ProductID=<!--@Ecom:Product.ID--><!--@If Defined(Ecom:Product.VariantID)-->", but response from the index doesn't contain PrimaryOrCurrentPageID. But even if I load product from Ecommerce by Id, it doesnt have that page id either. Where can I get it from?


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Hi Evaldas,

Rendering of links depends on some context information (current shop, current page, etc.).
When you are retrieving products using your own web api controller instead of a normal page view then you will have to provide the missing information somehow.

You can load the products by id and then try something like this...

var defaultGroup = product.GetDefaultGroupByShopId(currentShopId);

Ecom:Product.PrimaryOrFirstGroupID
var primaryOrFirstGroupId = (defaultGroup != null) ?  defaultGroup.Id : string.Empty;

Ecom:Product.PrimaryOrCurrentPageID
var primaryOrCurrentPageId = (defaultGroup != null && defaultGroup.Meta.PrimaryPageId > 0) ? defaultGroup.Meta.PrimaryPageId : currentPageId;

Best regards,
Morten

 

You must be logged in to post in the forum