Using fields in parts that are created in code

Nov 10, 2010 at 3:43 PM

How can I accomplish this? I can't find any examples that uses ContentFields as members of ContentParts that are created in code.

Nov 10, 2010 at 4:31 PM

That's probably because the scenario for it is not obvious: why would you want to do that? Why not add a property to the part instead? If you determine that your scenario is still better addresed with code-created fields, I think you can alter the part definition from a migration and call WithField. There are some examples in unit tests.

Nov 10, 2010 at 8:49 PM
Edited Nov 10, 2010 at 8:50 PM

I'm planning to build a (basic) image slider module and was thinking of the following structure for it:

  • Module: Slider
    • Feature: Slider
      • Widget: Slider
      • ContentPart: Slider
        • Effect:string (or enum)
        • AnimSpeed: int
        • PauseTime: int
        • ShowArrows: bool
        • ShowCaptions: bool
        • Image1:SliderImage
        • Image2:SliderImage
        • Image3:SliderImage
        • Image4:SliderImage
        • Image5:SliderImage
        • Image6:SliderImage
      • ContentField: SliderImage
        • url:string
        • caption:string


I thought that a custom field will be the solution for this because SliderImage contains two properties and custom logic (eg. upload an image or choose from the media folder). Or is it still a better idea to use normal properties? Thanks.

Nov 10, 2010 at 9:09 PM

Yes, I don't think fields are necessary here. the part should probably have a List<SliderImage> property (with some custom UI in the admin to manage such a list). SliderImage itself would be a plain class with virtual Url and Caption properties.