Add Video Player To SharePoint Content Editor WebPart Programmatically Using Server-Side Object Model

Introduction

You can add media such as video, audio or an image to a SharePoint page. The media file can come from your computer, from SharePoint, or from another location such as a file share. There are several different ways you can add video clips to a SharePoint page when you edit the page in the browser. You can add a video player to the page to play an individual video file. In this article I will explain, how to add Video Player to SharePoint Content Editor WebPart programmatically, using Server-Side Object Model. This will be applicable for all SharePoint On Premise platforms.

Pre-Requisites

  1. Open Visual Studio.
  2. Open New Project dialog box. Expand Office/SharePoint node and then choose SharePoint Solutions.

    ew Project dialog box

  3. Choose the SharePoint 2013 – Empty Project template. Name the project as MediaProject.

    Empty

  4. Choose the Deploy as a farm solution option button and choose Finish button.

    Deploy

  5. To create an Application page, choose MediaProject project.

    create

  6. On the menu bar, choose Project-> Add New Item.

    Add New Item

  7. In the Add New Item dialog box, choose the Application Page template.

    Application Page template

  8. Name the page AddMediaWebPart and choose Add button.

Add Video Player

  1. Open AddMediaWebPart.aspx file, we created.

    code

  2. Add the three text boxes for Page URL, Image URL and Video URL and one Submit button control to the PlaceHolderMain content control.
    1.    
    2. <h4>Add Video Player</h4>  
    3. <div>  
    4.     <label for="pageURL">  
    5.         <span class="required">*</span>Page URL:  
    6.     </label>  
    7.     <input type="text" id="pageURL" runat="server" />  
    8.     <label for="imageURL">  
    9.         <span class="required">*</span>Image URL:  
    10.     </label>  
    11.     <input type="text" id="imageURL" runat="server" />  
    12.     <label for="videoURL">  
    13.         <span class="required">*</span>Video URL:  
    14.     </label>  
    15.     <input type="text" id="videoURL" runat="server" />  
    16. </div>  
    17. <div>  
    18.     <button type="button" class="submit" id="AddWebPartSubmit" onserverclick="AddWebPartSubmitClick" runat="server">  
    19.         <span>  
    20.             <span>Submit</span>  
    21.         </span>  
    22.     </button>  
    23. </div>   
    code

  3. Open AddMediaWebPart.aspx.cs file.

    code

  4. Add the references, given below-
    1. using System;  
    2. using Microsoft.SharePoint;  
    3. using Microsoft.SharePoint.WebControls;  
    4. using Microsoft.SharePoint.WebPartPages;  
    5. using System.Xml;  
    6. using Microsoft.SharePoint.Publishing.WebControls;  
    7. using System.Web.UI.WebControls.WebParts;  
  5. Add AddWebPartSubmitClick event receiver for Submit button.
    1. protected void AddWebPartSubmitClick(object sender, EventArgs e) {}  
  6. Open the site and Web, using SPSite and SPWeb object.

  7. Check out the page and add Media Webpart.

  8. Access the Webparts place holder on the page by creating SPLimitedWebPartManager object.

  9. Create Content Editor Webpart and add HTML5 Video tag to the content of Content Editor Webpart.

  10. Set an image URL to poster attribute and video URL to source src attribute.

  11. AutoPlay option has been added to the video tag to auto play the video.
    1. try {  
    2.     using(SPSite site = new SPSite(pageURL.Value)) {  
    3.         using(SPWeb web = site.OpenWeb()) {  
    4.             SPLimitedWebPartManager webParts = null;  
    5.             webParts = web.GetLimitedWebPartManager(pageURL.Value, PersonalizationScope.Shared);  
    6.             //Adding Media WebPart   
    7.             ContentEditorWebPart contentEditorWPforMedia = new ContentEditorWebPart();  
    8.             XmlDocument addNewXMLDocforMedia = new XmlDocument();  
    9.             XmlElement contentXMLElementforMedia = addNewXMLDocforMedia.CreateElement("VideoContent");  
    10.             contentXMLElementforMedia.InnerXml = "<![CDATA[<video width='640' height='360' poster='" + imageURL.Value + "' controls='controls' autoplay>" + "<source src='" + videoURL.Value + "' type='video/mp4' /> " + "Your browser does not support the video tag." + "</video>]]>";  
    11.             contentXMLElementforMedia.InnerText = "<video width='640' height='360' poster='" + imageURL.Value + "' controls='controls' autoplay>" + "<source src='" + videoURL.Value + "' type='video/mp4' /> " + "Your browser does not support the video tag." + "</video>";  
    12.             contentEditorWPforMedia.Content = contentXMLElementforMedia;  
    13.             contentEditorWPforMedia.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;  
    14.             contentEditorWPforMedia.Content.InnerText = contentXMLElementforMedia.InnerText;  
    15.             webParts.AddWebPart(contentEditorWPforMedia, "BlankContent", 1);  
    16.             webParts.SaveChanges(contentEditorWPforMedia);  
    17.         }  
    18.     }  
    19. catch (Exception ex) {  
    20.     //Log the errors   
    21. }  
  12. Add the Media Webpart and save the page changes.

  13. Check in the page changes.

  14. Deploy the solution.

  15. Open the layouts AddMediaWebPart.aspx page, which we created.

    AddMediaWebPart

  16. Enter the Page URL, Image URL and Video URL values.

  17. Open the page and check the Video player is added to the page. Click Play button to play the video.

    video

Summary

Thus, you have learned, how to add Video Player to SharePoint Content Editor WebPart programmatically, using Server-Side Object Model.