Custom Content Type with Media?

Ed
Ed
Vote:
 

I am trying to create custom content/type for pages with downloadable media, such as a PDF. I want a few text fields, such as title and description of the file, and a way to upload a PDF, so that when the page is viewed, there'd be a way to view the PDF, via download, or somehow inline on the page.

#228923
Oct 05, 2020 18:41
Vote:
 

You can define first create a media type to support the pdf media -

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

namespace MyEpiserverSite.Models.Media
{
    [ContentType(DisplayName = "GenericMedia", GUID = "89761dbb-bf22-4cee-93c7-9d661d75cad8", Description = "Used for generic file types such as Word or PDF documents.")]
    [MediaDescriptor(ExtensionString = "pdf,doc,docx")]
    public class GenericMedia : 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; }

    }
}

Then on the pagetype, you can just define one property called - PdfMedia

                [CultureSpecific]
                [Editable(true)]
                [Display(
                    Name = "Pdf Media",
                    Description = "Add a media of the content.",
                    GroupName = SystemTabNames.Content,
                    Order = 10)]
                public virtual GenericPdfMedia PdfMedia{ get; set; }

Then just render this property on the view with its internal properties

@Model.PdfMedia.Title
@Model.PdfMedia.Description
#228964
Oct 06, 2020 7:20
Ed
Vote:
 

I'm not sure how this would actually work. Why would GenericPdfMedia work as a reference if we define above as GenericMedia? It doesn't, so I assume it was a typo, and change to GenericMedia. It compiles, but when I launch the site, I get:

Type 'XXX.XXXXXX.Models.Media.GenericMedia' could not be mapped to a PropertyDefinitionType

#229011
Oct 06, 2020 18:13
Vote:
 

No sure if this is excactly what you want, but to build on Ravindra's code...

On the pagetype, change to:

public virtual ContentReference PdfMedia{ get; set; }

In your view

@Html.PropertyFor(m => m.PdfMEdia)

In /Views/Shared/DisplayTemplates/ create a file PdfMedia.cshtml with the content:

@model EPiServer.Core.ContentReference

@if (Model != null)
{
    var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
    var pdf = contentLoader.Get<PdfMedia>(Model);

    <h1>@pdf.Title</h1>
    <strong>@pdf.Description</strong>
}
#229013
Edited, Oct 06, 2020 19:31
* 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.