Anto Subash.

orchard-core
cms
Orchard Core - Getting Started -Part : 3

Create content with code in Orchard Core - Part 3

Table of contents

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

dotnet new occms -n MyCms

Add a module to the cms with part

dotnet new ocmodulecms --name MyCms.Projects

Create a solution and add the projects

To create a solution

dotnet new sln -n ContentWithCode

To add projects to the solution.

dotnet sln add .\MyCms\MyCms.csproj
dotnet sln add .\MyCms.Projects\MyCms.Projects.csproj

To add Module as a reference to the project

dotnet add .\MyCms\MyCms.csproj reference .\MyCms.Projects\MyCms.Projects.csproj

Add migration

Create the migrations class Migrations.cs file in our module MyCms.Projects

using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;

namespace MyCms.Projects
{
    public class Migrations : DataMigration
    {
        IContentDefinitionManager _contentDefinitionManager;

        public Migrations(IContentDefinitionManager contentDefinitionManager)
        {
            _contentDefinitionManager = contentDefinitionManager;
        }

        public int Create()
        {
            _contentDefinitionManager.AlterTypeDefinition("Project", type => type
                .Draftable()
                .Versionable()
                .Creatable()
                .Securable()
                .Listable()
                .WithPart("Project")
            );

            _contentDefinitionManager.AlterPartDefinition("Project", part => part
                .WithField("Name", field => field
                    .OfType("TextField")
                    .WithDisplayName("Name")
                )
                .WithField("StartDate", field => field
                    .OfType("DateField")
                    .WithDisplayName("Start date")
                )
                .WithField("Image", field => field
                    .OfType("MediaField")
                    .WithDisplayName("Main image")
                )
                .WithField("Cost", field => field
                    .OfType("NumericField")
                    .WithDisplayName("Cost")
                )
            );

            return 1;
        }
    }
}

Update the start up file to add the migration.

services.AddScoped<IDataMigration,Migrations>();

Create a Headless Orchard CMS

Run the MyCms project.

dotnet run

You will see a Setup screen in the Recipe dropdown choose the Headless Site

Setup

Once the setup is complete then enable the MyCms.Projects module.

Features

Go to Features and you will see the list of all the features. Search for "MyCms.Projects" and enable it.

Enable

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.

New Projects

You will be able to create a new project now.

Create Projects

Buy Me a Coffee at ko-fi.com