Adding Custom fields through code

Posted by Community Admin on 04-Aug-2018 16:44

Adding Custom fields through code

All Replies

Posted by Community Admin on 18-Jan-2012 00:00

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

Posted by Community Admin on 21-Jan-2012 00:00

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);

Please note that if the type you'll be working with is already a dynamic type you won't need to execute the first part of the code. Once created you can operate with your custom field like this:
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");

You can also check the Products Catalog sample from our Sitefinity SDK and inspect how the fields for the module are created on module installation.

Please do not hesitate to let us know if you need any additional information or any further assistance on this.

All the best,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed