joshb
joshb

Reputation: 5220

Programatically creating a content type in Orchard CMS

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

Upvotes: 4

Views: 3968

Answers (2)

Sebastián Guerrero
Sebastián Guerrero

Reputation: 1114

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

Upvotes: 1

Bertrand Le Roy
Bertrand Le Roy

Reputation: 17814

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

Upvotes: 8

Related Questions