Can't attach Content Field to Content Type Definition programmatically...

Topics: Core, General, Writing modules
Mar 9, 2013 at 2:15 PM
Edited Mar 9, 2013 at 2:24 PM
I've tried to attach an existing Content Field to dynamically created Content Type, as describes in following code:
var contentTypeDefinition = "someContentItemIdentifier";
 _contentDefinitionManager.AlterTypeDefinition(contentTypeDefinition,
   cfg => cfg.WithPart("CommonPart"));
            
_contentDefinitionManager.AlterPartDefinition(contentTypeDefinition,
builder => builder.WithField("MyContentItemField", fieldBuilder => 
                 fieldBuilder.OfType("MyFieldType")
                .WithDisplayName("some display name")));
var contentItem = _contentManager.Create(contentTypeDefinition);
As result, i have custom ContentItem with name that i specified in variable "contentTypeDefinition" and no fields attached, and one more separated ContentPart with name specified in variable "contentTypeDefinition" and field "MyFieldType" attached. What's wrong in my code? Thanks for help!
Mar 9, 2013 at 3:40 PM
Edited Mar 9, 2013 at 4:38 PM
I was able to attach content field into Content Type by analogy with AdminController actions:
var contentTypeDefinitionName = "someContentItemIdentifier";
var contentTypeDefinition = _contentDefinitionService.AddType(contentTypeDefinitionName,
contentTypeDefinitionName);

_contentDefinitionService.AddPartToType("CommonPart", contentTypeDefinitionName);
_contentDefinitionService.AddPart(new CreatePartViewModel 
                                                           { Name = contentTypeDefinitionName });
_contentDefinitionService.AddPartToType(contentTypeDefinitionName, contentTypeDefinitionName);

var part = _contentDefinitionService.GetPart(contentTypeDefinitionName);
_contentDefinitionService.AddFieldToPart("MyContentItemField", "some display name", 
           "MyFieldType",part.Name);
Is it the right way to use?
Mar 10, 2013 at 5:01 AM
Edited Mar 10, 2013 at 5:02 AM
Sorry for stupid question. The answer was obvious. If anyone is interesting on it, result code placed below.
var contentTypeDefinitionName = "someContentItemIdentifier";
 _contentDefinitionManager.AlterTypeDefinition(contentTypeDefinitionName,
               cfg => cfg.WithPart("CommonPart"));
_contentDefinitionManager.AlterPartDefinition(contentTypeDefinitionName, 
            builder => builder.Attachable());
_contentDefinitionManager.AlterTypeDefinition(contentTypeDefinitionName, typeBuilder =>
       typeBuilder.WithPart(contentTypeDefinitionName));
_contentDefinitionManager.AlterPartDefinition(contentTypeDefinitionName,
       partBuilder => partBuilder.WithField("MyContentItemField", fieldBuilder => 
            fieldBuilder.OfType("MyFieldType").WithDisplayName("some display name")));