More in this section

Forums / Developing with Sitefinity / Adding Custom fields through code

Adding Custom fields through code

2 posts, 0 answered
  1. Robin
    Robin avatar
    15 posts
    03 Aug 2011
    18 Jan 2012
    Link to this post
    Is it possible to add custom fields through code?

    I want to create my widgets so that they can auto-configure themselves if necessary.

  2. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    30 Oct 2017
    21 Jan 2012
    Link to this post
    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();
    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);

    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
2 posts, 0 answered