Developer forum

Forum » Ecommerce - Standard features » Access Stored field from Index in Product list

Access Stored field from Index in Product list

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I know it might sound stupid, but I have to ask.

If I define a Grouping field in the index and set it to "Stored", is there any way of reading it in the template on the regular GetLoop("Products")?

Thank you,
Adrian 


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Adrian,

 

The Product Catalog App only gets back AutoIds from the Index, and then queries the Services layer (aka cache) and/or database to get the real data. So your options end up being:

  • Converting to the Query Publisher
    Which will give you the data from the index in tags, but you lose a few things like multiple sorting querystring parameters, facet options label from "List Box" fields, to say a few
     
  • Custom development to
    • get the index
    • query the data to get the specific document you're on the pdp
    • get the field you're looking for
       
  • Recreate the logic in a custom helper/function/block

 

I'd recommend the 3rd option. You can go with the 2nd, but I've never done quite like that, but here's some code that can get you the result of a query. It was built for Users, but it works just the same for Products

 

public static UserResult QueryUsers(string userName = "")
{
var queryService = Dynamicweb.Extensibility.ServiceLocator.Current.GetInstance<Dynamicweb.Indexing.Querying.IQueryService>();
var querySettings = new Dynamicweb.Indexing.Querying.QuerySettings();
var parameters = new Dictionary<string, object>();
parameters.Add("username", userName);
querySettings.Parameters = parameters;
var result = queryService.Query(queryService.LoadQuery("Users", "Users.query"), querySettings);
var counter = 0;
var user = new UserResult() {Results = new List<IDictionary<string, object>>()};
 
foreach (object resultObject in result.QueryResult)
{
var resultDictionary = resultObject as IDictionary<string, object>;
user.Results.Add(resultDictionary);
}
 
user.TotalCount = result.TotalCount;
return user;
}

 

var userLocations = QueryUsers();
if (userLocations.TotalCount > 0)
{
@foreach (var userLocation in userLocations.Results)
{
@userLocation["UserID"]"
}
}

 

Best Regards,

Nuno

 
Nicolai Pedersen
Reply

I would be careful to use 3rd option as it can be relatively slow - if you do that on a product list. Open, query, read the index on 30+ products is not super. If you choose this option you should query all the product documents in one go and store the result.

An alternative is to create a custom field on the product and make a scheduled task using the SQL add-in and put the value in that custom field. Then it can be indexed and it can be accessed from the product list for 'free'

BR Nicolai

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

Thank you very much for all suggestions.

Performance is clearly something I need to be careful with.

I will evaluate my options now that I have quite a few :)

Thank you very much,

Adrian

 

You must be logged in to post in the forum