[SharePoint 2010 – Developpement] Allow embedded youtube video in blog post content (as content editor webpart), with new embedded youtube code (iframe) for SharePoint 2010 blog post

What is that ?

Based on this excellent tutorial from kirk Evans : http://blog.evepoints.com/2010/11/add-video-sharepoint-2010-blog.html
This will allow you to add youtube video in a blog content.
Basic sharepoint feature permit you to add the video by modifying the HTML code, from the Insert HTML button of sharepoint, but that is not really user friendly.
Preview, on a blog post :


New youtube embedded format ?

I modified one of the method of Kirk Evans in order to accept the new youtube embedded video format. The new embedded video format looks like this :

New youtube embedded code for video (<iframe) :


Old youtube embedded code for video (object) :


So to update Kirk Evans code, replace the last method by :

   1: private string ReplaceEmbedsWithTokens(string body)
   2:         {
   3:             //VARS
   4:                 string returnString = null;
   5:                 //Old version of youtube embedded
   6:                 Regex rx = new Regex("(&lt;object).*?(&lt;embed src=&quot;)(?<src>.*?)(&quot;)(.*?)(&lt;/object&gt;)");
   8:             //CLEANING from a text editor
   9:             body = body.Replace("nr", " ");
  10:             body = body.Replace("rn", " ");
  12:             if (rx.IsMatch(body) == false)
  13:             {
  14:                 //New version of youtube embedded
  15:                 rx = new Regex("(&lt;iframe).*?(src=&quot;)(?<src>.*?)(&quot;)(.*?)(&lt;/iframe&gt;)");
  16:                 returnString = rx.Replace(body, "[[BLOGVIDEO: ${src}]]");
  17:                 returnString = returnString.Replace("embed", "v");
  18:             }
  19:             else
  20:             {
  21:                 //Old version of youtube embedded
  22:                 returnString = rx.Replace(body, "[[BLOGVIDEO: ${src}]]");
  23:             }
  26:             return returnString;
  27:         }

It also fix a bug : when you paste the embedded code in a word document in order to save it, and then you paste it in a blog, it won’t work properly.

Enjoy, and thanks again Kirk Evans


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: