Adding a MediaLibraryPickerField to Custom Part

Topics: General
Nov 10, 2013 at 1:39 PM
Edited Nov 10, 2013 at 1:40 PM

In order to learn about how to add a MediaLibraryPickerField to a part, i'm creating a simple customer module where each customer will have a name, an email and an image.

Here's my Content Part Record:
 public class CustomerPartRecord : ContentPartRecord
        public virtual string Name { get; set; }
        public virtual string Email { get; set; }
Here's my Content Part:
 public class CustomerPart : ContentPart<CustomerPartRecord>
        public string Name
            get { return Record.Name; }
            set { Record.Name = value; }

        public string Email
            get { return Record.Email; }
            set { Record.Email = value; }
And to add a media field where a customer can upload an image, we need that put into migration according to many blog posts.

Here's my Migration:
 public int Create()
            // Customer table will have Name, Email, CustomerImage fields
            SchemaBuilder.CreateTable("CustomerPartRecord", table => table

            ContentDefinitionManager.AlterPartDefinition("CustomerPart", part => part
                .WithField("CustomerImage", field => field
                    .WithDisplayName("Customer Image")));

            ContentDefinitionManager.AlterTypeDefinition("Customer", type => type.WithPart      ("CustomerPart"));

            return 1;
The above migration runs successfully as verified by the table record below from
Orch.Framework.DataMigrationRecord table:

32 MyName.CustomersWithImage.Migrations 1

In the admin menu, when I check in the Parts repository, I see my customer part.

When I click "Edit" on the customer part, i'm getting the following exception:
The model item passed into the dictionary is of type 'Orchard.MediaPicker.Settings.MediaPickerFieldSettings', but this dictionary requires a model item of type 'Contrib.MediaPickerField.Settings.MediaPickerFieldSettings'.

Need some help with the above as to what is going wrong.

Also had some general questions about adding an image:
  1. Is the MediaPickerField obsolete? and should we use MediaLibraryPickerField ?
  2. I'm assuming if the latter is required, we need to add a reference to Orchard.MediaLibrary and also modify Module.txt to have a Dependency on: Orchard.MediaLibrary... is this correct?
Any advice is appreciated.
Nov 11, 2013 at 7:31 AM
  1. Yes, MediaPickerField is deprecated and you should use MediaLibraryPickerField instead.
  2. Yes
Marked as answer by robroysd on 11/11/2013 at 3:56 AM