How to Add Video Embed Webpart on SharePoint Office 365 Modern pages using C# CSOM Programmatically


Sathish Nadarajan
SharePoint MVP
Published On :   08 Jan 2019
Visit Count
Today :  4    Total :   152


In this article, let us see how to add a Video Embed Webpart Programmatically using C# in Office 365 Modern Pages.

1. Add the Video to the Video APP.

clip_image002

clip_image004

2. Upload the Videos.

3. Get the URL of the Videos

4. Execute the below Piece of code.

 public static void AddVideoEmbedWebPartToModernPage()
         {
             try
             {
                 OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
                 using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                 {
                     var page2 = ctx.Web.AddClientSidePage("PageWithSections.aspx", true);
                     page2.AddSection(CanvasSectionTemplate.ThreeColumn, 5);
                     page2.Save();
 
 
                     ClientSideWebPart videoEmbedWp = page2.InstantiateDefaultWebPart(DefaultClientSideWebParts.VideoEmbed);
 
                     videoEmbedWp.Properties["videoSource"] = "https://sppalsmvp.sharepoint.com/portals/hub/_layouts/15/PointPublishing.aspx?app=video&p=p&chid=****&vid=*******";
                     videoEmbedWp.Properties["captionText"] = "video";
                     videoEmbedWp.Properties["showInfo"] = false;
                     //videoEmbedWp.Properties["embedCode"] = "<iframe width=853 height=480 src='https://sppalsmvp.sharepoint.com/portals/hub/_layouts/15/PointPublishing.aspx?app=video&p=p&chid=******&vid=*********&amp;width=853&amp;height=480&amp;autoPlay=false&amp;showInfo=true' allowfullscreen></iframe>";
                     videoEmbedWp.Title = "Associated video";
 
                     CanvasSection section = page2.Sections[0];
                     page2.AddControl(videoEmbedWp, section.Columns[1], 0);
                     page2.Save();
                 }
             }
             catch (Exception ex)
             {
                 System.Console.WriteLine("Exception occurred : " + ex.Message);
                 System.Console.ReadLine();
             }
         }
 

Happy Coding,

Sathish Nadarajan.

Categories