The mysteries of Orchard’s OEmbedPart, or how to integrate YouTube videos
The media library picker field does a fine job of integrating and rendering external videos such as YouTube videos. If you want to customize the rendering of the videos, however, you’ll have to deal with the OEmbedPart (obtainable from contentItem.As<OEmbedPart>()). OEmbed is a fairly relaxed protocol that is designed to enable the integration of bits of a site into another. It can deal with images, videos, or even arbitrary HTML. Because it is so vague in what it can do, it’s rather hard to give it a representation other than something completely dynamic, and that's exactly what OEmbedPart is doing. Its only structured property is Source, which is the URL of the original content, one of the only constants in the data. The rest is available through the part’s indexer, by name: part[“title”] for example.
Here’s the format of the JSON video response that sites like YouTube return, copied from the oEmbed web site:
"title": "Amazing Nintendo Facts",
"<object width=\"425\" height=\"344\">
<param name=\"movie\" value=\"http://www.youtube.com/v/M3r2XDceM6A&fs=1\"></param>
<param name=\"allowFullScreen\" value=\"true\"></param>
<param name=\"allowscriptaccess\" value=\"always\"></param>
type=\"application/x-shockwave-flash\" width=\"425\" height=\"344\"
While one can use the Source property of the part to recover the original URL of the contents, the indexer allows access to each of the properties of the oEmbed document, such as “html”, the html to embed into the page to render the rich contents:
All other properties are also there for you to use. Many sites, YouTube included, will give more than what the oEmbed spec require however, so I encourage you to look for what you need in the stored document that can be found in the Infoset (the Data column of the Orchard_Framework_ContentItemRecord table, under the Data/OEmbedPart path). Here’s an example of that:
<html><iframe width="480" height="270" src="http://www.youtube.com/embed/4I5Q3UXkGd0?feature=oembed" frameborder="0" allowfullscreen></iframe></html>
<title>What Is The Resolution Of The Eye?</title>
As you can see, there are quite a few additional properties, in particular “thumbnail_url”, “thumbnail_width”, and “thumbnail_height”, which can help you render a placeholder image, which is very useful when rendering in a list (the cost of rendering each video as an actual iframe would be prohibitive in those circumstances).