Add arbitrary number of assets (pdf, doc, etc) as a property to a content type?

Ed
Ed
Vote:
 

So I have this content type I set up. Each piece of content has a description and a few text fields, but I also need to allow for an arbitrary number of assets to be added. For example, a user needs to be able to add a PDF, then have the opportunity to add a second, third, etc. 

This is what I'm doing now. I have a media content type that looks like this:

using System;
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Framework.DataAnnotations;

namespace XXX.XXXXXX.Models.Media
{
[ContentType(DisplayName = "GenericMedia", GUID = "xxxxxxxx-bf22-4cee-93c7-xxxxxxxxx", Description = "Used for generic file types such as Word or PDF documents.")]
[MediaDescriptor(ExtensionString = "pdf,doc,docx")]
public class GenericPdfMedia : MediaData
{
[CultureSpecific]
[Editable(true)]
[Display(
Name = "Title",
Description = "Add a title of the content.",
GroupName = SystemTabNames.Content,
Order = 10)]
public virtual String Title { get; set; }

[CultureSpecific]
[Editable(true)]
[Display(
Name = "Description",
Description = "Add a description of the content.",
GroupName = SystemTabNames.Content,
Order = 20)]
public virtual String Description { get; set; }

}
}
And then I have a content type that uses it like this:
[CultureSpecific]
[Editable(true)]
[Display(
Name = "Images / Attachments",
Description = "Add a media asset for the content.",
GroupName = SystemTabNames.Content,
Order = 400)]
public virtual ContentReference PdfMedia { get; set; }
What do I need to do to let admins add potentially one PDF after another? I don't even know where to begin.
#229185
Edited, Oct 08, 2020 18:26
Vote:
 

Just use a list, and perhaps add an UIHint.

[CultureSpecific]
[Display(
   Name = "Images / Attachments",
   Description = "Add a media asset for the content.",
   GroupName = SystemTabNames.Content,
   Order = 400)]
[UIHint(UIHint.MediaFile)]
public virtual IEnumerable<ContentReference> PdfMedia { get; set; }

And, to allow PDF only, create a MediaType for PDF, and set allowed types like this

[AllowedTypes(typeof(PdfMedia))]

A ContentArea could also do the trick.

#229187
Edited, Oct 08, 2020 19:01
Vote:
 

While Tomas's answer is very good (with the note to AllowedTypes which is helpful), it's worth noting that it should be IList<ContentReference> instead of IEnumerable<ContentReference>

#229225
Oct 09, 2020 14:10
Tomas Hensrud Gulla - Oct 10, 2020 5:41
Quan, could you please explain WHY it should be IList instead of IEnumerable. I’m quite sure they both will work, but are there reasons to favour one over the other?
Quan Mai - Oct 10, 2020 6:24
I stand corrected :). In older versions it has to be IList, but apparently in later version you can use IEnumerable or ICollection. However I think using IList allows you to add or remove items easier using code.
Tomas Hensrud Gulla - Oct 11, 2020 8:22
:-)
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.