Friday, November 2, 2012

Embed video in SharePoint 2010 blog posts - the easy way

2nd Edit: Added a link to the script file as copying from this page introduces illegal characters
NEW source file supporting multiple browsers!

Edit: Added some more explanation around the Content Editor and the script.

Embedding videos to the Body of a blog post in SharePoint has always required some tricks and coding.
If you Google the internet you may come accross posts like Embedding Flash Video in SharePoint Blogs by Kirk Evans and the ERTE project on Codeplex by Boris Gomiunik.

I've taken some inspiration in both these approaches, but wanted a solution that didn't include adding code to masterpages or installing solutions. Simplicity should be the key word. So my solution makes it possible to paste the embed code directly into the body text and hit Publish.And get the video working. To do this you simply add a Content Editor web part (CEWP) to the bottom of your page.

To edit the HTLM source of the CEWP (click Edit page and) click in the Editor part of the web part
Now in the ribbon locate the HTML icon, click the dropdown arrow and click on Edit HTML Source
Once in the HTML Source editor add the below script and click OK:

You can find the script file here: EmbedVideo.txt

Once the HTML Source editor is closed, click Page, Stop Editing.

After you done this the script should NOT show up on the page, if it does, then is has not been successfully entered into the HTML Source, examine it for any illegal characters that might occur from copying it from this page.

Once the script is successfully entered into the HTML of the CEWP you should be ready to move on!

Now you can paste the embed code directly into body text or you can even include embedded videos to items on a custom list or wherever you want...



And the result:



Or even in a custom list or as here a Link list using a Enhanced Rich Text field:

So, happy embedding :)

49 comments:

  1. I am really interested in learning more about how this works. I have tried placing the code in several places and still can't seem to get it to work. I really need to be able to embed and use iframes in a custom list. i'm not trying on a blog but a custom list which has a rich text field for publishing. any suggestions?

    ReplyDelete
    Replies
    1. Hi Kristin. Did you add a Content Editor Web part to your page? And added the script to the HTML source by clicking HTML, Edit HTML Source? And finally, could you show me your embed code that you have added to the rich text field in your custom list?

      Delete
    2. I am displaying information from a list using the empty data view. i added the above code into the master page for the custom aspx page where the data view is. i then tried to use the embed code from a youtube video placing it directly into the body of a "Full HTML content with formatting and constraints for publishing" field.

      Delete
    3. Ok, well, the script has been developed to fit the scenario of a rich text field and it is looking for a class of the name 'ExternalClass' before it looks for the iframe tag. All this is done (and the reason why I didn't go for at master page solution is) to optimize performance. Now if you know a little scripting you could remove some of the check or you could use IE developer tools (F12) to figure out which tags you have in your scenario. Or maybe go with some of the masterpage solutions I link to in the top of this post.

      Delete
    4. A colleague shared your solution with me recently - thanks so much! I was a big fan of Boris' ERTE code pre-SP2010, but LOVE the way this works too! Thanks! :)

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Sorry was being slow and have copied your code into the HTML of a CEWP. The code shows when I look at the content of a blog post but no video appears. Embed code:

    iframe width="560" height="315" src="http://www.youtube.com/embed/ZJbw3zYRaVA" frameborder="0" allowfullscreen></iframe

    ReplyDelete
  4. Hello, I am using the code below and it doesnt work. I have followed your instruction carefully.

    the code im embedding is not allowed for submission on this page.

    ReplyDelete
  5. I'm sorry that it's not working for you, I editted my post with more details around the CEWP. Please let me know if this helps you.

    ReplyDelete
  6. Sorry, I tried adding the CEWP with the code you provided in the html dialog but it does not work. I added your code to NewForm, EditForm and DisplayForm but still no video content displaying.

    ReplyDelete
  7. Dear all

    I just tried using the code from this web page and guess what? It failed! Turns out that you'll get illegal characters when you copy from here, so I removed the source window and added a link to the file directly.

    Please retry with the correct script, thank you.

    ReplyDelete
  8. Hi Jesper,
    Excellent work .It works fine for me. I just included your code in the master page (i'm using the CKS:EBE extension) of my blog site. Tested with IE8 and IE9.
    I get an javascript error with Firefox and i don't see why. under investigation.
    Gerald

    ReplyDelete
    Replies
    1. The innerText is not supported by fx. a possible workaround is to use the contentText instead.
      Gerald

      Delete
    2. Thank you Gerald! I've updated the script to support multiple browsers using the technique from http://blog.coderlab.us/2006/04/18/the-textcontent-and-innertext-properties/
      Please test is and let me know

      Delete
  9. Thank you, thank you, thank you! Tried numerous other scripts without luck and this one finally worked... and it was by far the simplest!

    ReplyDelete
  10. This code worked wonderfully for me and I thank you very much for posting such an easy solution to a complex problem. My one problem is that any text in the entry that appears below the video does not appear in the post. Am I doing something that would make that occur or is this an issue with the code. Thank you for any answer you may have and thanks again for the code!

    ReplyDelete
    Replies
    1. Good to hear that it worked. The code should leave any text below the video alone, so I'll need a little more information about how you do, which browser you use, the text you have inserted in the Body field and maybe a screenshot.

      Delete
    2. I am having a few issues with my email service, which means I cannot get those screenshots at this time. If this helps, I had simple text encased within paragraph HTML coding. The video was a .wmv file and we currently have IE8 deployed on our computers. I will try to straighten out the email issue I am experiencing and have some screen shots to give you at a later time. Thank you for responding!

      Delete
    3. Hello again! I did resolve my issue, although I am not certain exactly what was occurring. I have a suspicion it involved an issue with our group's branding that caused the glitch. Thank you again for providing such a helpful resolution to our needs!

      Delete
  11. It worked well the first time ! Amazing ! You are a genius !

    ReplyDelete
  12. This actually worked and took me 2 minutes. Do you have any idea how many hours I have spent trying to get all of those ridiculously complicated ways people have posted to work (and they never do of course)? This is bloody brilliant. Tremendous hats off to you sir. This is just brilliant. Simple, effective and well communicated. Thank you!

    ReplyDelete
  13. Thanks for the solution...worked for me..

    ReplyDelete
  14. Any possibility one can embed an iframe form in Sharepoint 2010?

    ReplyDelete
    Replies
    1. Hi Mark. I got no clue, never tried it before. I do see mr. Google raise some concerns about forms in iframes, but you could try it as the script does handle iframe tags.

      Delete
  15. Thank you for the work. I have tried your solution and it did not work for me. I paste the code into the CEWP and then save it I get the following messge - Warning: The HTML source you entered might have been modified.
    How do you get around this

    ReplyDelete
    Replies
    1. Hi 2824cops. Sorry to hear it didn't work. The warning you mention is 'normal' and shouldn't effect the script. You can check that it hasn't altered the script by re-opening the HTML Source code window as per description above, copy everything out to a new Notepad and compare it to the original script. The differences should be in formatting, linefeeds etc. A good tool for comparing text files is WinDiff. If there are differences you could try re-adding the original script. If the content of the HTML Source is identical to the original script I would like to get some details on how it didn't work. E.g. how does the embed code you are putting into the Blog post or Rich Text field look? And maybe some screenshots of how your page look after you added the script. You can find my email in my complete profile.

      Delete
  16. Thanks for posting this for the world to use!

    I've tried using this script and am not able to make it fully work.

    I'm using this script on a Branded Web Part Page, so I've adjusted the class selector as appropriate (instead of externalclass).

    When I load the page (with the iframe text in a CEWP and the script included as a Web Part), the iframe text disappears after an instant, but I do not see an embed appear on the page.

    I've looked through the script, and I don't understand where in the code the embed actually gets put onto the page. Is it done simply through the unescape?

    ReplyDelete
    Replies
    1. Actually, I have now tried the script with a video embedded from YouTube in an iframe, and that works just fine.

      However, a website (such as https://www.google.com/) embedded in an iframe does not work.

      Delete
    2. Nevermind.... I think the problem lies in the way that my company has security and Sharepoint set up.

      I can successfully embed a YouTube video and even this web page, but for some reason I can't embed Google or some other web pages.

      Anyway, thanks for the script!

      Delete
  17. Thank you for this. I am able to embed videos from youtube. How can I embed videos from my local drive?

    ReplyDelete
    Replies
    1. You could just upload them to YouTube and then embed them this way

      Delete
    2. Or maybe upload them to a document library on SharePoint and embed them from there.

      Delete
  18. A colleague shared your solution with me recently - thanks so much! I was a big fan of Boris' ERTE code pre-SP2010, but LOVE the way this works too! Thanks! :)

    ReplyDelete
  19. AMAZING WORK! I had a little trouble making it work at first, I was trying to link to the text file instead of just copying the script in the CWP. But with a little help from my coworker we got it to work. Thank you!!

    ReplyDelete
  20. Perfect, easy to implement solution - made my marketing team very happy. Cheers!

    ReplyDelete
  21. im trying this solution but is not working, and im getting this messages into console:

    Uncaught TypeError: Cannot read property 'className' of null

    Uncaught TypeError: Cannot set property 'name' of null

    ReplyDelete
    Replies
    1. Hello Salvador. Sorry to hear it not working for you. As you can see from the other comments it is working for most people, so it would be interesting to know you setup. Which browser and version are you using? Which version of SharePoint? And which type of document library? Any other info? If you know a little debugging try hitting F12 and examine the area around the embed code

      Delete
    2. hi, i already fixed the errors but still not working, tried using an alert inside the if that execute the embedvideo function but it is not showing:

      if (window['_spBodyOnLoadFunctionNames'])
      {
      _spBodyOnLoadFunctionNames.push("embedVideo");
      }
      else
      {
      embedVideo();
      alert('embedvideoworks');
      }


      about your questions:
      sharepoint 2010 standard
      document library: posts
      other info: Services Pack 1 With Cumulative Update June 2016

      Delete
    3. Try putting the alert into the start of the embedVideo function as it may be failing before it completes. Which browser are you using? IE x.xx? Also the column you are putting the embed code into has to be a rich text field like the Body of a blog post, otherwise the script may not find the needed fields

      Delete
    4. tried the alert at start, it shows the alert.

      about rich text field, you are right the column is not rich text field, how can this be changed?

      im using chrome and IE11

      Delete
    5. Go to Library settings, click on the column, choose multiline + rich text, not plain text and not enchanced

      Delete
    6. verified and it is already rich tex field

      Delete
    7. hey, i did this and worked:

      <_script type="text/javascript">
      $('div[class*=ExternalClass]').each(function(){
      var $this = $(this);
      var t = $this.text();
      $this.html(t.replace('&lt','<').replace('&gt', '>'));
      });

      ​​​​

      but is only working in desktops, tried on android and is not working

      Delete
    8. i have an update, it is working on android, it was a ssl problem :D

      Delete
    9. Perfect! Nice to know and thanks for reporting your solution back.

      Delete
  22. This comment has been removed by the author.

    ReplyDelete
  23. I'm having trouble getting this to work right. I added the CEWP and put in the code. Now, when I create a blog post with the embedded iframe code from YouTube, all I get is a black rectangle (no video). I'm using SharePoint 2010 Enterprise, a SharePoint blog site, the Body field is rich text, and I'm trying to view it using Internet Explorer 11. Any ideas?

    ReplyDelete
    Replies
    1. Sorry to hear that, no I don't really have any ideas, maybe try looking at Compatibility settings or try using e.g Chrome, just to see if that works

      Delete
  24. YOU ARE MY HERO!!!!!!!!! REALLY, the ONLY solution that worked for me in Sharepoint 2010. I was about to throw myself out of the window after spending hours with the other solutions. Amazing!!! Thank you!

    ReplyDelete