This project is read-only.

ContentDefinitionManager not working

Topics: Customizing Orchard, Troubleshooting
Oct 18, 2011 at 3:50 PM

Hi All,

I am creating a custom part where, in the settings for that part, depending on a value the user selects from the dropdown, the system attaches a certain (other) part to the content type that the custom part is on.

In the 

TypePartEditorUpdate(ContentTypePartDefinitionBuilder builder, IUpdateModel updateModel)

method of my settings class, I am calling:
_definitionManager.AlterTypeDefinition("GroupsUserContent", bld => bld.WithPart("GroupsUserContent"));

(_definitionManager is an instance of IContentTypeDefinitionManager, the names are strings for simplicity here, but will be variable)
Its not working. The part is not attached to the type. Nothing is added to the database in the 'Settings_ContentTypePartDefinitionRecord' table.
The names of the types/parts definately exist. I can call the exact same line of code from the modules Migrations.cs file and it works as expected.
Anyone have any clues as to why this isn't working?
Oct 18, 2011 at 6:46 PM

Altering types should be done in a migration. That's it. You can still dynamically add parts to any item (from a handler for example), but you do that on the item, not by altering the type it was built from.

If you still want to do this by altering the type, look at how the content type editor is doing it.

Oct 18, 2011 at 9:39 PM

Hi Bertrand,

Thanks for your response. The reason I want to do this using types is so that the admin user can create the type simply by attaching 1 part and selecting a setting, and my custom code will attach the correct additional parts. Then the editor users can create items of that type with all the correct parts attached.

I think I will have to settle for doing this by just telling my admin user to attach the correct parts based on what they need. However, it would be nice to know why this doesn't work. 

Apologies if I misundertand, by 'the content type editor' do you mean the pages in the admin section where you can attach fields/parts to types? I have looked through the code for these page, and this seems to be calling IContentDefinitionManager in the same way I am. Please correct me if I'm wrong.



Oct 19, 2011 at 1:24 AM

This doesn't work because the shell doesn't restart and take your changes into account I would assume. Would be easy to check by restarting the app. The content type editor is doing it though so if you want to know how it does it, you'll have to look in there. Deeper than you have already.

Also, did you consider recipes?