Using ContentManager in Migrations

Topics: Writing modules
Jul 24, 2013 at 11:59 PM
Edited Jul 25, 2013 at 12:05 AM
I am trying to create new records for the following content parts, but cannot seem to be able to create records using the Content Manager.

I cannot get something like this to work:
 ContentDefinitionManager.AlterTypeDefinition("MyObject", builder =>
                builder
                    .Creatable()
                    .WithPart("CommonPart")
                    .WithPart("TitlePart")
                    .WithPart("AutoroutePart", part =>
                        part
                        .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                        .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
                        .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Winning Numbers Title', Pattern: 'winning-numbers/{Content.Slug}', Description: 'winning-numbers/winning-numbers-title'},{Name:'Winning Numbers Title', Pattern:'winning-numbers/{Content.Slug}', Description:'winning-numbers/winning-numbers-title'}]")
                        .WithSetting("AutorouteSettings.DefaultPatternIndex", "0")
                        ));

            SchemaBuilder.CreateTable("MyObjectPartRecord", table => table
                .ContentPartRecord()
                .Column<string>("Date"));

            ContentDefinitionManager.AlterTypeDefinition("MyObject", builder => builder.WithPart("MyObjectPart"));

            ContentDefinitionManager.AlterPartDefinition("MyObjectPart", builder => builder.Attachable());

            DateTime beginDate = DateTime.Now.AddDays(-90);

            MyObjectPart p = MyObjectBuilder.GetNumber(DateTime.Now);

            _contentManager.Create(p.ContentItem, VersionOptions.Published);
Coordinator
Jul 25, 2013 at 7:16 AM
How does it not work? Please give details about what you are trying to do, what you tried, and how it failed.
Jul 25, 2013 at 2:09 PM
Edited Jul 25, 2013 at 2:10 PM
I have tried it so many different ways, each not working. I think this particular way one of the p properties comes back as null.

It is only the ContentManager I have issues w/.
Coordinator
Jul 25, 2013 at 7:32 PM
Try to do your content manager manipulations in a separate migration step. Creating content items from a migration can be problematic, in particular using types that you just created from the same step. Migrations are not really designed for content creation, but more for schema changes. Alternatively, you may want to move this to a recipe, if that makes sense.
Jul 26, 2013 at 4:27 PM
I am following you on what you say. It makes sense.

I am trying to do what you suggest and I am getting a null exception on the following:
var record = _contentManager.New("MyObjectPart").As<MyObjectPart>();
record.Date = DateTime.Now;
record is coming back as null. I am not sure what else needs to be done to instantiate the record object.

Thanks!
Coordinator
Jul 26, 2013 at 9:31 PM
The argument for New is the content type name, not the part name.