How to get the datatype of metafields in Episerver 10

Vote:
 

We have soem dynamic set of properties/metafields that we want to update from code behine.

Before updating the fields we want to check the datatype, validate the data and then update the value in the field. 

In episerver 10 , metafields are added in "Properties" property of each object (linetiem, catalogentry, orderform, ect) as a dictionary. So it only contains the name of the fields and value.

Is there a way to get the "datatype"  of the metafield. 

We had this in episerver 9 which use to work just fine
currItem.MetaClass.MetaFields[key].DataType;

We now have this, which DOSEN"t work
currItem.Properties[key].GetType();
This doesn't work. When the value is null for the given metafield it throws error.

#178094
May 01, 2017 17:05
Vote:
 

Not sure if this by any means is a recommended way of doing it with the new interface based order system. But this is what I found while looking around:

if (currItem is ILineItem)
{
    var type = OrderContext.Current.LineItemMetaClass.MetaFields[key].DataType;
}

// There are many more of these but here are some examples:
OrderContext.Current.PurchaseOrderMetaClass.MetaFields[key].DataType;
OrderContext.Current.ShoppingCartMetaClass.MetaFields[key].DataType;
OrderContext.Current.OrderAddressMetaClass.MetaFields[key].DataType;
OrderContext.Current.ShipmentMetaClass.MetaFields[key].DataType;


You probably already know what "currItem" is so you might not need the if-clause.

#178113
Edited, May 02, 2017 11:19
Vote:
 

That totally worked. Thank you!

Do you think this can cause a performance issue by any means?

#178138
May 02, 2017 18:51
Vote:
 

"Do you think this can cause a performance issue by any means?"

OrderContext.Current.LineItemMetaClass is used in the constructor of the concrete LineItem class. In other words, the MetaClass-property you used before v10 was built up using that same call to OrderContext.Current.LineItemMetaClass. :)

In either case the data seems to be cached after the first time it gets picked up from the DB. I'd assume that it's safe to say that the performance hit would be negligeble or non-existant even if you keep recalling OrderContext.Current.LineItemMetaClass.

#178151
Edited, May 03, 2017 9:40
Vote:
 

Thank you for explaining that! :) 

Great, we are good then here. 

#178181
May 03, 2017 17:39
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.