Add Media Webpart To SharePoint Page 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 Media Web Part to the page to play an individual video file. In this article, I will explain how to add Media Webpart to SharePoint Page programmatically, using Server-Side Object Model. This is applicable to all SharePoint On Premise platforms.

Pre-Requisites

  1. Start Visual Studio.

  2. Open the New Project dialog box, expand the Office/SharePoint node under the language that you want to use, and then choose the SharePoint Solutions node.

    new

  3. In the Visual Studio Installed Templates pane, choose the SharePoint 2013 – Empty Project template. Name the project MediaProject, and then click the OK button.

    empty

  4. Choose the Deploy as a farm solution option button, and then click the Finish button to accept the default local SharePoint site.

    Deploy

  5. To create an application page in Solution Explorer, choose the MediaProject project.

    Solution Explorer

  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 (Farm Solution Only) template.

    Add New Item

  8. Name the page AddMediaWebPart, and then choose the Add button.

Add Media Webpart

  1. Open the AddMediaWebPart.aspx file we created.

    AddMediaWebPart

  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. <h4>Add Media WebPart</h4>  
    2. <div>  
    3.     <label for="pageURL">  
    4.         <span class="required">*</span>Page URL:  
    5.     </label>  
    6.     <input type="text" id="pageURL" runat="server" />  
    7.     <label for="imageURL">  
    8.         <span class="required">*</span>Image URL:  
    9.     </label>  
    10.     <input type="text" id="imageURL" runat="server" />  
    11.     <label for="videoURL">  
    12.         <span class="required">*</span>Video URL:  
    13.     </label>  
    14.     <input type="text" id="videoURL" runat="server" />  
    15. </div>  
    16. <div>  
    17.     <button type="button" class="submit" id="AddWebPartSubmit" onserverclick="AddWebPartSubmitClick" runat="server">  
    18.         <span>  
    19.             <span>Submit</span>  
    20.         </span>  
    21.     </button>  
    22. </div>    
    AddMediaWebPart

  3. Open the AddMediaWebPart.aspx.cs file.

    AddMediaWebPart

  4. Add the following references.
    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 the Submit button.
    1. protected void AddWebPartSubmitClick(object sender, EventArgs e)      
    2. {      
    3. }   
  6. Open the site and web using SPSite and SPWeb object.
  7. Check out the page going to add media webpart.
  8. Access the webparts place holder on the page by creating SPLimitedWebPartManager object.
  9. Create the new unique id using GUID object.
  10. Create MediaWebPart object and assign following webpart properties.

    • MediaSource – Video source URL.
    • AutoPlay – To enable auto play option.
    • Heigth- Height of video player.
    • Width – With of video player.
    • BackImageUrl – Back image of video in player.
    • PreviewImageSource – Preview image for video player.
    • Set ChromeState and ChromeType for webpart property.
    • ID – Unique id of webpart.
      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.             Guid storageKeyMedia = Guid.NewGuid();  
      8.             string wpIdMedia = String.Format("g_{0}", storageKeyMedia.ToString().Replace('-''_'));  
      9.             MediaWebPart mediaWebPart = new MediaWebPart {  
      10.                 MediaSource = videoURL.Value,  
      11.                     AutoPlay = false,  
      12.                     Height = 360,  
      13.                     Width = 640,  
      14.                     BackImageUrl = imageURL.Value,  
      15.                     PreviewImageSource = imageURL.Value,  
      16.                     ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal,  
      17.                     ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None,  
      18.                     ID = wpIdMedia  
      19.             };  
      20.             webParts.AddWebPart(mediaWebPart, "BlankContent", 1);  
      21.             webParts.SaveChanges(mediaWebPart);  
      22.         }  
      23.     }  
      24. catch (Exception ex) {  
      25.     //Log the errors    
      26. }  
  11. Add the Media webpart and save the page changes.

  12. Check in the page changes.
  13. Deploy the solution.
  14. Open the layouts AddMediaWebPart.aspx page that we created.

    layouts

  15. Enter the Page URL, Image URL, and Video URL values.
  16. Open the page and check the Media webpart added to the page. Click on play button to play the video.

    video

Summary: Thus, you have learned how to add Media Webpart to SharePoint Page programmatically, using Server-Side Object Model.

Up Next
    Ebook Download
    View all
    Learn
    View all