How do we populate a ContentField through the Orchard Command Prompt?

Topics: General
May 12, 2013 at 12:44 AM
Edited May 12, 2013 at 12:48 AM
We have the following command. It succeeds at creating a DealerProduct and populating the Title ContentPart. We have not, though, been able to populate a ContentField. How would we go about that?
[CommandName("dealerdashboard createproduct")]
public void Create()
{
    // make a new DealerProduct - this works
    var dealerProduct = _contentManager.New("DealerProduct");

    // populate the title - this works
    dealerProduct.As<TitlePart>().Title = "Test" + i.ToString();

    // try to populate a simple text field - doesn't work
    DealerProductPart dealerProductPart = 
        dealerProduct.As<DealerProductPart>();
    TextField partNum = 
        (TextField)dealerProductPart.Get(typeof(TextField), "Part #");
    partNum.Value = "123";
        
    // create the new DealerProduct - this works
    _contentManager.Create(dealerProduct);
}
Developer
May 12, 2013 at 1:31 PM
Are you sure the Name of the field is "Part #"?
May 13, 2013 at 6:34 PM
Edited May 13, 2013 at 6:34 PM
This is the code that creates the field, and it appears to have the Name "Part #". Am I mistaken?

Here is a snippet:
.WithField("Part #", field => field.OfType("TextField"))
and here is the full code.
public int Create()
{
    // Creating table DealerProductRecord
    SchemaBuilder.CreateTable("DealerProductRecord", table => table
        .ContentPartRecord()
    );

    ContentDefinitionManager.AlterPartDefinition(
        typeof(DealerProductPart).Name, cfg => cfg
            .Attachable()
            .WithField("Image 1", field => field.OfType("ImageField"))
            .WithField("Image 2", field => field.OfType("ImageField"))
            .WithField("Part #", field => field.OfType("TextField"))
            .WithField("Model #", field => field.OfType("TextField"))
            );

    ContentDefinitionManager.AlterTypeDefinition("DealerProduct", cfg => cfg
        .WithPart("DealerProductPart")
        .WithPart("CommonPart", cp => cp
            .WithSetting("OwnerEditorSettings.ShowOwnerEditor", "false")
            .WithSetting("DateEditorSettings.ShowDateEditor", "false"))

        .WithPart("BodyPart")
        .WithPart("TitlePart")
            .Draftable()
            .Creatable()); 
                
    return 1;
}
Developer
May 13, 2013 at 7:18 PM
Ok. So, the following line of code does return a field?
TextField partNum =  (TextField)dealerProductPart.Get(typeof(TextField), "Part #");
On the next line you're setting its value. Perhaps explain what exactly is not working. What are you expecting, and how is it failing?
May 13, 2013 at 8:25 PM
Edited May 13, 2013 at 8:29 PM
Hi Sipke,

Thank you for your help.

Yes. That line of code does return a field.

What we are expecting is this, which we get when adding the Part # through the GUI.
Image

What we are getting is this, which is what we get when adding the Part # through the command line.
Image

Here is the database result too. It seems that the GUI populates the [Orchard_Framework_ContentItemVersionRecord] table whereas the command line populates the [Orchard_Projections_StringFieldIndexRecord] table.
Image
May 14, 2013 at 11:41 PM
Hi there,

Has there been any success in populating a ContentField through the DefaultOrchardCommandHandler?

Cheers.
Shaun