Enterprise software development and consulting for various organizations, industries and domains.

Sunday, November 28, 2010

Adding video to a SharePoint 2010 blog

SharePoint 2010 blog engine does not allow in insert videos in a blog post.

There is a simple way to work around this limitation. Create new post and insert video embed code. I took one from youtube.com for example:


SharePoint will escape all HTML characters when record is being saved in a database. What we have to do is to register a javascript function which will replace escaped HTML back with the original one. I used this jquery code snippet:

<script type="text/javascript">
$(document).ready(function() {
 $(".ms-PostBody").html(function(index, oldHtml) {
  var matchStr = /(\&lt;object.+\/object\&gt;)/i;
  var m = oldHtml.match(matchStr);
 
  $.each(m, function (index, value) {
   oldHtml = oldHtml.replace(value, value.replace(/\&gt;/g, ">").replace(/\&lt;/g, "<"));
  });

  return oldHtml;
 });
});
</script>

It can be either added in a Content Editor web part on the page or registered via feature in a AdditionalPageHead delegate control or added to the master page using SharePoint designer.

Here is the result, enjoying YouTube videos in our blog.


For the security reasons, we might need to limit object codes to be videos only or videos from youtube only. In that case we will need to update regular expression used in the script which captures embed HTML.