Table of contents
- Intro
- Create a CMS
- Add a module to the cms with part
- Create a solution and add the projects
- Add migration
- Create a Headless Orchard CMS
Intro
In this post we will see how to create content types using the code in Orchard Core. We will see how to create content types with different parts and fields. We will also see how to create content items using the code.
Create a CMS
1dotnet new occms -n MyCms
Add a module to the cms with part
1dotnet new ocmodulecms --name MyCms.Projects
Create a solution and add the projects
To create a solution
1dotnet new sln -n ContentWithCode
To add projects to the solution.
1dotnet sln add .\MyCms\MyCms.csproj 2dotnet sln add .\MyCms.Projects\MyCms.Projects.csproj
To add Module as a reference to the project
1dotnet add .\MyCms\MyCms.csproj reference .\MyCms.Projects\MyCms.Projects.csproj
Add migration
Create the migrations class Migrations.cs
file in our module MyCms.Projects
1using OrchardCore.ContentManagement.Metadata; 2using OrchardCore.ContentManagement.Metadata.Settings; 3using OrchardCore.Data.Migration; 4 5namespace MyCms.Projects 6{ 7 public class Migrations : DataMigration 8 { 9 IContentDefinitionManager _contentDefinitionManager; 10 11 public Migrations(IContentDefinitionManager contentDefinitionManager) 12 { 13 _contentDefinitionManager = contentDefinitionManager; 14 } 15 16 public int Create() 17 { 18 _contentDefinitionManager.AlterTypeDefinition("Project", type => type 19 .Draftable() 20 .Versionable() 21 .Creatable() 22 .Securable() 23 .Listable() 24 .WithPart("Project") 25 ); 26 27 _contentDefinitionManager.AlterPartDefinition("Project", part => part 28 .WithField("Name", field => field 29 .OfType("TextField") 30 .WithDisplayName("Name") 31 ) 32 .WithField("StartDate", field => field 33 .OfType("DateField") 34 .WithDisplayName("Start date") 35 ) 36 .WithField("Image", field => field 37 .OfType("MediaField") 38 .WithDisplayName("Main image") 39 ) 40 .WithField("Cost", field => field 41 .OfType("NumericField") 42 .WithDisplayName("Cost") 43 ) 44 ); 45 46 return 1; 47 } 48 } 49}
Update the start up file to add the migration.
1services.AddScoped<IDataMigration,Migrations>();
Create a Headless Orchard CMS
Run the MyCms
project.
1dotnet run
You will see a Setup screen in the Recipe
dropdown choose the Headless Site
Once the setup is complete then enable the MyCms.Projects
module.
Go to Features and you will see the list of all the features. Search for "MyCms.Projects" and enable it.
Once the module is enabled then we can start creating our content.
Go to Content Items
page you will see a New Projects
menu now.
You will be able to create a new project now.