Adding Custom fields through code
Is it possible to add custom fields through code?
I want to create my widgets so that they can auto-configure themselves if necessary.
Thanks,
Robin
Hi Robin,
Yes, it's possible to add a metafield to virtually any type of dynamic content. Theoretically, you can convert almost any type to DynamicType and then add custom fields to it, so it might be possible for Taxon(the base class from which or classifications derive) as well. For example here's how you can add custom field for taxonomies:
var metaManager = Telerik.Sitefinity.Data.Metadata.MetadataManager.GetManager();
metaManager.CreateMetaType(
typeof
(Taxon));
metaManager.SaveChanges();
App.WorkWith().DynamicData().Type(
typeof
(Taxon)).Field().TryCreateNew(
"Test"
,
typeof
(String)).SaveChanges(
true
);
var manager = TaxonomyManager.GetManager();
var myCat = manager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name ==
"cat1"
).SingleOrDefault();
var testValue =
"Some value 1111"
;
myCat.SetValue(
"Test"
, testValue);
myCat.GetValue<String>(
"Test"
);