I'm creating an Orchard module and as part of my migration I need to create a new content type. I am able to inject an instance of IContentManager into my migration class which allows me to create new content items but I haven't been able to figure out how to create a new content type.
Can someone describe how this is done (code examples would be great)?
You don't need to inject content manager. There are many, many examples of that throughout the code. In fact, you can take pretty much any migration, in any module, and find something like this:
ContentDefinitionManager.AlterTypeDefinition("BlogPost",
cfg => cfg
.WithPart("BlogPostPart")
.WithPart("CommonPart", p => p
.WithSetting("DateEditorSettings.ShowDateEditor", "true"))
.WithPart("PublishLaterPart")
.WithPart("RoutePart")
.WithPart("BodyPart")
);
Ey, here you have another sample of part and content type definition.
// PART DEFINITION
ContentDefinitionManager.AlterPartDefinition("ProductPart", p => p
.WithDescription("Product part")
// To select other contents ( on this example "ProductPresentation" content type )
.WithField("ProductPresentation", f => f
.OfType("ContentPickerField")
.WithDisplayName("Product presentation")
.WithSetting("ContentPickerFieldSettings.Required", "True")
.WithSetting("ContentPickerFieldSettings.Multiple", "True")
.WithSetting("ContentPickerFieldSettings.ShowContentTab", "True")
.WithSetting("ContentPickerFieldSettings.Hint", "Please select product's presentation")
.WithSetting("ContentPickerFieldSettings.DisplayedContentTypes", "ProductPresentation")
)
// To select images
.WithField("ProductImageField", f => f
.OfType("MediaLibraryPickerField")
.WithDisplayName("Product image")
.WithSetting("MediaLibraryPickerFieldSettings.Hint", "Please select product's image")
.WithSetting("MediaLibraryPickerFieldSettings.Required", "True")
.WithSetting("MediaLibraryPickerFieldSettings.Multiple", "False")
)
// To add a taxonomy field called "ProductLines"
.WithField("Category", fcfg => fcfg
.OfType("TaxonomyField")
.WithDisplayName("Category")
.WithSetting("TaxonomyFieldSettings.Taxonomy", "ProductLines")
.WithSetting("TaxonomyFieldSettings.LeavesOnly", "True")
.WithSetting("TaxonomyFieldSettings.Required", "True")
.WithSetting("TaxonomyFieldSettings.SingleChoice", "False")
.WithSetting("TaxonomyFieldSettings.Autocomplete", "False")
.WithSetting("TaxonomyFieldSettings.AllowCustomTerms", "False")
.WithSetting("TaxonomyFieldSettings.Hint", "Please select product's category")
)
.Attachable()
);
// CONTENT TYPE DEFINITION
ContentDefinitionManager.AlterTypeDefinition("Product", cfg => cfg
.DisplayedAs("Product")
.WithPart(
"CommonPart", c => c
.WithSetting("DateEditorSettings.ShowDateEditor", "False")
.WithSetting("OwnerEditorSettings.ShowOwnerEditor", "False")
)
.WithPart("ProductPart")
.WithPart("TitlePart", c => c.WithSetting("Hint", "Please enter the product name"))
.WithPart("BodyPart", c => c.WithSetting("Hint", "Please enter the product description"))
.WithPart("LocalizationPart")
.WithPart("AutoroutePart", partBuilder => partBuilder
.WithSetting("AutorouteSettings.AllowCustomPattern", "true")
.WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "true")
.WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Product Title', Pattern: 'product/{Content.Slug}', Description: 'product/title-product'}]"))
.Listable()
.Draftable()
.Creatable()
.Securable()
.Indexed()
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With