Adding Field to BlogPost

Topics: Writing modules
Jan 30, 2013 at 8:05 PM

We have a module that requires an additional field be added to the BlogPost ContentType. Doing this manually through the admin works fine however I'd like the module's migration to do this.

I have the following but it doesn't work and the field doesn't get added to the BlogPost:

ContentDefinitionManager.AlterPartDefinition("BlogPost", builder =>
    builder.WithField("DisplayName", cfg =>
        cfg
            .OfType("TextField")
            .WithDisplayName("Display Name")
        )
);

Jan 30, 2013 at 9:14 PM

Fields attach to parts, not types. "BlogPost" is not a part. Try changing that to " .AlterPartDefinition("BlogPostPart", builder => "

Jan 31, 2013 at 7:44 AM
Thanks, that does work which is great.

However, if I add a Field to the BlogPost type from the Dashboard the field appears to be on the Type itself. I've since read that in that scenario a new Part is automagically created in the background with the same name as the BlogPost type.

Am I able to recreate this behaviour with a migration or is that not the recommended practice?
Jan 31, 2013 at 8:03 AM
To answer my own question, yes it's possible:
ContentDefinitionManager.AlterTypeDefinition("BlogPost",
                cfg => cfg
                    .WithPart("BlogPost"));

ContentDefinitionManager.AlterPartDefinition("BlogPost", builder =>
    builder.WithField("DisplayName", cfg =>
        cfg
            .OfType("TextField")
            .WithDisplayName("Display Name")
            ));