Search code examples
orchardcms

ContentManager.Create does nothing


I am trying to build a service in Orchard that allows me to create content through a custom form on a page. The service and the content type definitions look fine to me, but somehow, eventhough I don't get any errors or other signs in the Orchard log files, creating new content using the IContentManager does nothing for me.

Parts involved

The controller accepting the form values

[HttpPost]
public ActionResult Create(CreateSopViewModel viewModel)
{
    if(!ModelState.IsValid)
    {
        var shape = _shape.CreateContent();
        shape.Header = _shape.Parts_Title(Title: "New item");

        // Add the original fields to the shape.
        shape.Title = viewModel.Title;
        shape.Description = viewModel.Description;
        shape.InitialComments = viewModel.InitialComments;

        return new ShapeResult(this, shape);
    }

    // Store the new procedure in the database
    _service.CreateContentItem(
        viewModel.Title,viewModel.Description,viewModel.InitialComments);

    // Redirect the user back to the homepage.
    return Redirect("~/");
}

The service that contains the CreateContentItem method:

public void CreateContentItem(string title, string description, string initialComments)
{
    // Initialize a new content item based on the SOP type
    var customPart = _services.ContentManager.New<MyCustomPart>("CustomContentType");

    customPart.Description = description;
    customPart.Identifier = BuildIdentifier(title);
    customPart.ContentItem.As<TitlePart>().Title = title;

    _services.ContentManager.Create(customPart.ContentItem);
}

The content part + record

public class MyCustomPart: ContentPart<MyCustomPartRecord>
{
    [Required]
    public string Identifier
    {
        get { return Record.Identifier; }
        set { Record.Identifier = value; }
    }

    [Required]
    public string Description
    {
        get { return Record.Description; }
        set { Record.Description = value; }
    }
}

public class MyCustomPartRecord: ContentPartRecord
{
    public virtual string Identifier { get; set; }

    public virtual string Description { get; set; }
}

The migration

SchemaBuilder.CreateTable(typeof(MyCustomPartRecord).Name, table => table
    .ContentPartRecord()
    .Column<string>("Description")
    .Column<string>("Identifier"));

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

ContentDefinitionManager.AlterTypeDefinition("CustomContentType", builder => builder
    .DisplayedAs("Custom Content Type")
    .WithPart("TitlePart")
    .WithPart("MyCustomPart")
    .Creatable(true));

Question

Again, I don't get any errors, not in the log and not in Visual Studio. However, my new content item doesn't get created or at least, I can't see it in the admin section of the site under Content.

What is going on and how can I debug this behavior?


Solution

  • I had a similar problem, which was solved when I used the overloaded Create method taking a VersionOptions enum value:

    content.Create(customPart.ContentItem, VersionOptions.Published);

    This should work even if the content item is not creatable, as mine isn't.