API
Version 7.x > API > How to access new product type fields using API Kentico version 7 View modes: 
User avatar
Member
Member
scott-corlnet - 1/21/2014 2:01:20 PM
   
How to access new product type fields using API Kentico version 7


I have created a new product document type in Kentico in site manager / document types. I added new fields such as size and color. In the ecommerce tab I selected "Document type represents a product type". This created a new table for my new document type in the database with the custom fields.

I am able to add a new content item of this type in my products section in cms desk / content. But in my code, I am not sure how best to use the API in order to access the custom fields I created for this document type. When it was just a product I would use the following:

CMS.Ecommerce.SKUInfoProvider.GetInfoById(PredefinedObjectType.SKU, id)

I can still get the sku info for my new product type using this method, but I do not see how I can access the custom fields I created for my new product type.

Also, I am not sure how Kentico is tying the new table with my custom fields for this product type to the COM_SKU table

User avatar
Certified Developer 13
Certified Developer 13
kentico_josefd - 1/22/2014 6:31:18 AM
   
RE:How to access new product type fields using API Kentico version 7
Hello Scott,

Fields defined as part of a Document Type are not directly accessible from the SKU Info Object. This is due to a fact that SKUs can be used by multiple documents, with different values in this field.

You will need to specify which specific document's field you would like to access, like this:

CMS.DocumentEngine.TreeNode node = TreeHelper.SelectSingleNode("<NodeAliasPath or NodeID>");
if (node != null)
{
string result = ValidationHelper.GetString(node.GetValue("CustomField"), "Invalid string");
}


Or you will need to cycle through all documents which may be, but do not have to be, linked to the SKU. You can find all related documents based on the NodeSKUID. Please note, that you need to specify a single Document Type, otherwise Document Type fileds will not load:

TreeNodeDataSet nodes = TreeHelper.SelectNodes("/Products/%", false, "CMS.Product", "NodeSKUID = 1183");
if (!DataHelper.DataSourceIsEmpty(nodes))
{
foreach (var row in nodes)
{
string value = ValidationHelper.GetString(row.GetValue("CustomField"), "Invalid string");
}
}


Let me know if you need any further help.


Regards,

Josef