Can't add field to content type during migration successfully?

Topics: Writing modules
Mar 15, 2012 at 12:53 AM
Edited Mar 15, 2012 at 12:55 AM

Hey guys, here is my code:

        public int UpdateFrom2()
        {
            ContentDefinitionManager.AlterTypeDefinition("Project", t => t
                .WithPart("TitlePart")
                .WithPart("CommonPart")
                .WithPart("ContainablePart")
                .WithPart("TagsPart")
                .Creatable()
                );

            ContentDefinitionManager.AlterPartDefinition("Project", builder =>
                builder.WithField("History", cfg =>
                    cfg
                        .OfType("TextField")
                        .WithDisplayName("History")
                )
            );
            return 3;
        }

Basically, the History field wont display when I use the admin gui to view it? Have I got the content type name correct when adding the field ["project"], or should it be something like "projecttype"? Is there any documentation on this [...I'm really struggling to find any decent documentation on Orchard???] thanks for you thoughts, pg

Coordinator
Mar 15, 2012 at 6:13 AM

You don't actually have the Project part on the Project type.

Mar 15, 2012 at 7:46 AM

I thought I read somewhere that when you create a contenttype - a dummy ContentPart with the same name is created as well – is this not the case?  And if not, are you saying there is no way to replicate the GUI (...which can simply add fields to a contenttype) via migration, but to create a projectpart with all the required fields, then add that part to my projecttype?
Thanks for your help, pg

Mar 15, 2012 at 10:44 AM

Because you defined a new part, you'll need to add this part to the Project type.

.WithPart("Project")
Mar 15, 2012 at 11:04 AM

Ok, as in:

            ContentDefinitionManager.AlterPartDefinition("Project",
                part => part
                    .WithField("History", field => field.OfType("TextField"))
                    );
            
            ContentDefinitionManager.AlterTypeDefinition("Project", t => t
                .WithPart("TitlePart")
                .WithPart("CommonPart")
                .WithPart("Project")
                .WithPart("ContainablePart")
                .WithPart("TagsPart")
                .Creatable()
                );

This works as expected. Cheers Guys