Saving Data to Custom Fields on Product
I am having an issue where I have created a Custom Field on a
Product Type called "Sample". The name of this field is DetailField.
I have a product of Product Type Sample and I would like to save a block
of text to this Custom Field of my Product. I have tried
This corrupts the prodcuct Data:
Product.SetValue(“DetailStringResources”, “<b>my html formatted string
here</b>”);
This throws an Error “Not a PersistenceCapable instance or type
was not enhanced.”: Product.SetFieldValue(“DetailStringResources”,
“<b>my html formatted string here</b>”);
What am I missing and what is the proper way to set that field
to the value that I want? I did attempt to find API documentation related
to this but there was nothing that addressed custom fields on products (via
Product Type) Thanks!
Hello Walt,
We have already provided you with a response in the support ticket you had open on the same topic. For your convenience please find our response quoted below as well.
About the first paragraph you have wrote I guess you are creating this custom field from the backend right? not using some code ? The code for creating a custom field is here : http://www.sitefinity.com/documentation/documentationarticles/developers-guide/sitefinity-essentials/modules/ecommerce/product-types/custom-fields/creating-custom-fields/creating-long-text-fields but I guess you are doing the creation from the backend. If so everything in the creating of the field should be correct, if you haven't mark it as hidden can you confirm it is visible on the product edit screen ?
it should look like see beforeChange.png attached bellow.
In order to use this field via the API (which is your second question) you need to do the fallowing :
//lines just to get the product I want to be updated
var manager = CatalogManager.GetManager();
var productType = manager.GetProductTypes().Where(pt => pt.TitlePlural == "Sample").Single();
var firstSampleProduct = manager.GetProducts(productType.ClrType).First(); // just take one
//important lines for the custom field change.
firstSampleProduct.SetValue("DetailStringResources", "changed via API call");
manager.SaveChanges();
Then you can verify it is changed as it is shown in afterChange.png attached bellow.
Documentation for that can be found here http://www.sitefinity.com/documentation/documentationarticles/developers-guide/sitefinity-essentials/modules/ecommerce/product-types/custom-fields/setting-the-value-of-a-custom-field
I hope it is clear and working now on your side.
Please let me know if you need future assistance.
Regards,
Nayden Gochev
Telerik