0
votes

I am working on an Orchard site that needs to be able to use some customized forms to create new content items.

To handle this I'm using a controller to display a form and then trying to create the new content items on post back by populating the dynamic items and then sending them through the ContentManagerService's Create() function.

This is working ok until I got to the content picker field I have as part of my content item.

In my project I have a content type of Question Record that has a SubmittedBy field that is a Content Picker Field.

Here is what I can see in the immediate window while processing the post back:


    > dynamic q = _questionService.NewQuestion("Why doesn't this work?");
    {Custom.Website.Models.Question}
        base {Orchard.ContentManagement.ContentPart}: {Custom.Website.Models.Question}
        IsNew: true
        OriginalQuestion: "Why doesn't this work?"
        Summary: null
    > q.QuestionRecord
    {Orchard.ContentManagement.ContentPart}
        base {System.Dynamic.DynamicObject}: {Orchard.ContentManagement.ContentPart}
        ContentItem: {Orchard.ContentManagement.ContentItem}
        Fields: Count = 5
        Id: 0
        PartDefinition: {Orchard.ContentManagement.MetaData.Models.ContentPartDefinition}
        Settings: Count = 0
        TypeDefinition: {Orchard.ContentManagement.MetaData.Models.ContentTypeDefinition}
        TypePartDefinition: {Orchard.ContentManagement.MetaData.Models.ContentTypePartDefinition}
        Zones: {Orchard.UI.ZoneCollection}
    > q.QuestionRecord.SubmittedBy
    {Orchard.ContentPicker.Fields.ContentPickerField}
        base {Orchard.ContentManagement.ContentField}: {Orchard.ContentPicker.Fields.ContentPickerField}
        ContentItems: null
        Ids: {int[0]}

The ContentItems property is read-only and the Ids when assigning a new int[] to the Ids array I get a System.ObjectDisposedException with the message: Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.

Are there any workarounds to get this value set in code or do I need to create my own property to store the related content item ids? It would be very helpful to have the admin interface of the ContentPickerField also available.

Thanks.

1
Using the immediate window can be challenging sometimes, so it seems like you're adding problems by using it in this case. You can access a field once you've cast an item to dynamic: item.ContentTypeName.FieldNameBertrand Le Roy
Thanks for your response. I'm able to access the field ok whether through immediate window or code, in this case the SubmittedBy field. Where I am getting stuck is that the field is a ContentPickerField part and I can't find a way to programmatically assign it a value.CtrAltDel

1 Answers

2
votes

If you have a reference to the ContentPickerField, you can assign it a value using the Ids property. In example (assuming your content type has a part called Question which has a field called SubmittedBy):

var submittedByField = ((dynamic)q.ContentItem).Question.SubmittedBy;
sbmittedByField.Ids = new[] { submittedById };

As Bertrand mentioned, the format to access a content field is: contentItem.PartName.FieldName. If you attached a field to a type directly via the admin, the part name has the same name as the type name, hence contentItem.TypeName.FieldName (where TypeName is actually the name of the implicitly created part).