BlogEngine.NET YouTube Extension - with Facebook Integration

I've found this great YouTube extension for BlogEngine.NET from Al Bsharah. I've extended it a bit so I could integrate the YouTube Video into Facebook. I've already extended the MediaElementPlayer for Facebook Integration. If you read that article, you almost know everything.

In short words: there are only a few things to change in the YouTubePlayer.cs from Al Bsharah. First, add this new method:

 

private static void AddMetaData(string propertyName, string content)
{
    if (string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(content))
        return;

    if (HttpContext.Current.CurrentHandler is System.Web.UI.Page)
    {
        System.Web.UI.Page pg = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
        HtmlGenericControl metaTag = new HtmlGenericControl("meta");
        metaTag.Attributes.Add("property", propertyName);
        metaTag.Attributes.Add("content", content);
        pg.Header.Controls.Add(metaTag);
    }
}

 

We're already done: Find the Post_Serving Eventhandler and add following lines at the bottom within the for-loop:

 

if (e.Location == ServingLocation.SinglePost)
{
    //add facebook properties only for single post
    Post post = sender as Post;

    AddMetaData("og:url", "http://www.youtube.com/watch?v=" + mediaFile);
    if (post != null)
    {
        //use the post title
        AddMetaData("og:title", post.Title);

        //use the post description
        AddMetaData("og:description", post.Description);
    }
    AddMetaData("og:type", "video");

    //default video thumbnail
    AddMetaData("og:image", "http://img.youtube.com/vi/" + mediaFile + "/2.jpg");

    AddMetaData("og:video", BaseURL + mediaFile + "&autoplay=1&fs=1");
    AddMetaData("og:video:type", "application/x-shockwave-flash");
    AddMetaData("og:video:height", "259");
    AddMetaData("og:video:width", "398");
}

 

For a detailed description of the attributes and how to integrate videos direct into Facebook, please have a look at my article mentioned above. The video thumbnail url is the default thumbnail for the video. Attached you could find the modified YouTubePlayer.cs originally written by Al Bsharah.

YouTubePlayer.cs (7.65 kb)

Important Note: There are a few limitations:

  • It only works for single posts. But it will be simple to extend it for pages
  • You have to fill out the "Excerpt (optional)" when writing a post in BlogEngine.NET otherwise the video will not be recognized by Facebook
  • It only works with one video in one post. If you've multiple videos, only the first will be shared in Facebook

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

Something about the author

Month List