Create ContentItem and ContentPart from Code Dynamically.

Topics: Core, Writing modules
Feb 10 at 8:24 PM
Edited Feb 10 at 8:25 PM
Hi, this does not work. Any clue about how to do it?

Migrations:
ContentDefinitionManager.AlterPartDefinition("MomPart", builder => builder
               .Attachable()
               .WithField("MomName", cfg => cfg.OfType("TextField")
                   .WithDisplayName("Name")
                   .WithSetting("TextFieldSettings.Flavor", "Large")
                   .WithSetting("TextFieldSettings.Required", false.ToString()))
               .WithField("MomDate", cfg => cfg.OfType("DateTimeField")
                   .WithDisplayName("DateTime")
                   .WithSetting("FieldIndexing.Included", true.ToString())
                   .WithSetting("DateTimeFieldSettings.Required", true.ToString())
                   .WithSetting("DateTimeFieldSettings.Display", "DateAndTime"))
               .WithField("MomPrice", cfg => cfg.OfType("TextField")
                   .WithDisplayName("Price")
                   .WithSetting("TextFieldSettings.Flavor", "Wide")
                   .WithSetting("TextFieldSettings.Required", false.ToString()))
               .WithField("MomUnits", cfg => cfg.OfType("TextField")
                   .WithDisplayName("Units")
                   .WithSetting("TextFieldSettings.Flavor", "Wide")
                   .WithSetting("TextFieldSettings.Required", false.ToString()))
               .WithField("MomLastPrice", cfg => cfg.OfType("TextField")
                   .WithDisplayName("Last Price")
                   .WithSetting("TextFieldSettings.Flavor", "Wide")
                   .WithSetting("TextFieldSettings.Required", false.ToString()))
               );

            ContentDefinitionManager.AlterTypeDefinition("Mom", cfg => cfg
               .DisplayedAs("Mom")
               .WithPart("CommonPart")
               .WithPart("MomPart")
               .WithPart("IdentityPart")
               .WithPart("TitlePart"));
Content Item is created successfully, the TitlePart is created successfully, MomPart does not work. The MomPart Info is not stored.
```
        dynamic mom = Services.ContentManager.New("Mom");
        dynamic part = mom.MomPart;
        part.MomDate.DateTime = DateTime.Now;
        part.MomName.Value = "mamita";
        part.MomPrice.Value = "0";
        part.MomLastPrice.Value = "9489";
        part.MomUnits.Value = "4";
        mom.TitlePart.Title = "mami";
        Services.ContentManager.Create(mom);
the part reference is successfully extracted from mom content item. When the create function is called, some magic happens and the reference part is gone.
I think it has to do with the persistence of mom in db. I would use flush method if it was a member of ContentManager, but it is missing in V1.7.2.

Any clue?

Thx!
Feb 10 at 9:07 PM
Edited Feb 10 at 9:10 PM
Ok, i got the solution!

I injected ITransactionManager into the class that was creating the content-item, and i called transaction.RequireNew() method before asigning new content-part info.
            dynamic mom = Services.ContentManager.New("Mom");
            Services.ContentManager.Create(mom);
            transaction.RequireNew();
            dynamic part = mom.MomPart;
            part.MomDate.DateTime = DateTime.Now;
            part.MomName.Value = "mamita";
            part.MomPrice.Value = "0";
            part.MomLastPrice.Value = "9489";
            part.MomUnits.Value = "4";
            mom.TitlePart.Title = "mami";
Sometimes the fact of writing your own concern gives you some new perspective to solve the problem. I had to dive into the code to figure it out how to force a commit. If there is a better way to do it please tell me how!

Thx!