Adding fields to the "User" Content Type with Migrations file

Topics: Customizing Orchard
Jan 8, 2013 at 9:52 PM

I am writing a module that we want to use the User Content Type and need to customize this Content Type by adding 2 fields to it.  

My code is as follows and seems to add the proper rows in the Settings_ContentPartFieldDefinition.  

ContentDefinitionManager.AlterPartDefinition("User", cfg => cfg.Attachable());

ContentDefinitionManager.AlterPartDefinition("User", builder => builder.WithField("DisplayName", cfg => cfg
																									   .OfType("TextField")
																									   .WithDisplayName("Display Name")
																									   .WithSetting("TextFieldSettings.Flavor", "Wide")
																									   .WithSetting("TextFieldSettings.Required", "False")
																									   ));

ContentDefinitionManager.AlterPartDefinition("User", builder => builder.WithField("GoogleAccount", cfg => cfg
																									   .OfType("TextField")
																									   .WithDisplayName("Google+ Account")
																									   .WithSetting("TextFieldSettings.Flavor", "Wide")
																									   .WithSetting("TextFieldSettings.Required", "False")
																									   ));
Now here is the odd part.  If I go add a dummy field to the User before I enable my module it works fine.  If I don't do this the fields don't show up.  Is this a bug or am I not adding something to make this work.

Coordinator
Jan 9, 2013 at 2:40 AM

Looks like it.

Developer
Jan 9, 2013 at 11:05 AM

If you attach a field to the User content type via the admin, a "User" part will be created and attached to the "User" content type.
If you don't do that but only execute your migration, a "User" part will be created, but it won't be attached to the "User" content type.
If you add a line that attaches the "User" part to the "User" type, it should work.

Developer
Jan 9, 2013 at 11:08 AM

By the way, creating a content part with a name that does not end in "Part" will not show up in the list of parts, regardless of the "Attachable" setting. So your first line in your migration will not cause the "User" part to appear in the list of parts in the backend.

Jan 9, 2013 at 10:17 PM

Thanks that makes perfect sense now.  

Really enjoying learning the product.