Youtube Integration with SharePoint

Hey Geeks

after long time i am writing this post due to busy schedule. recently i got the opportunity to integrate SharePoint with YouTube i had the requirement to Develop the Videos gallery control and videos should be fetch from YouTube channel.  here are the steps.

first you need to register these three DLLs in your solution in Package.Package file(you can download these DLLs from MyOneDriveShareFiles) then Let’s create a class to represent YouTube video on our control. The object of this class is a video that has properties: VideoId, Title, PublishDate, VideoDuration.  these properties will help us to show the publishing date of the video, Title of the video, and duration of the video, …ETC..there are lots of other properties in the dlls that can help you to more customize the functionality. if you want to make webpart configurable so you need to create webpart properties.

To use all possibilities that YouTube Data API offered, you need to get Developer Key.  Developer key uniquely identifies an application that will interact with the YouTube service. You can get it Here

string YOUTUBE_CHANNEL –> WebPart Property for YouTube channel name

string YOUTUBE_DEVELOPER_KEY –> WebPart Property for YouTube developer Key(Optional)

 



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Client;
using Google.YouTube;

namespace AlQasmia.Utility_Functions
{
    public class YouTubeVideoObject
    {
        public string VideoId { get; set; }
        public string Title { get; set; }
        public string PublishDate { get; set; }
        public string VideoDuration { get; set; }
    }
    public class YouTubeVideoHelper
    {
       // const string YOUTUBE_CHANNEL = "BobinaMusic";
        //const string YOUTUBE_DEVELOPER_KEY = "AI39si6JqO_f2b_JWSV3QUbcwg5S-1RJ4-kiieosBZy9r1ORkCAv7BT5tLp579Tzmly8rvOVm3Jyueq3ZVqUNt1blS4DcoVppA";

        public static YouTubeVideoObject[] GetVideos(string YOUTUBE_CHANNEL, string YOUTUBE_DEVELOPER_KEY)
        {
           
                YouTubeRequestSettings settings = new YouTubeRequestSettings("Channel", YOUTUBE_DEVELOPER_KEY);
                YouTubeRequest request = new YouTubeRequest(settings);

                string feedUrl = String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads?orderby=published", YOUTUBE_CHANNEL);
                Feed videoFeed = request.Get(new Uri(feedUrl));

                return (from video in videoFeed.Entries
                        select new YouTubeVideoObject() { VideoId = video.VideoId, Title = video.Title, PublishDate = video.YouTubeEntry.Published.Date.ToShortDateString(), VideoDuration = video.Media.Duration.Seconds}).ToArray();
           
           
        }
    }
}


Now its time to bind videos with Repeater control. you can use grid as well.

<asp:Repeater ID="VideosRepeater" runat="server">
    <ItemTemplate>
    
             
Publish Date:<%# Eval(“PublishDate”) %> Duration:<%# Eval(“VideoDuration”)%> (Seconds)
    </div>
    </ItemTemplate>
    <SeparatorTemplate>
      <br />
    </SeparatorTemplate>
    </asp:Repeater>
    <div class="button">
    
    <asp:ImageButton ID="ImgBtnPrev" runat="server" CssClass="button" OnClick="LnkBtnPrev" ImageUrl="/_layouts/images/prev.png" />
    <asp:ImageButton ID="ImgBtnNext" runat="server" CssClass="button" OnClick="LnkBtnNext" ImageUrl="/_layouts/images/next.png" />

Below you can find the Repeater Control Code.


 public YouTubeChannel ParentWebPart
        {
            get;
            set;
        }

        public int PagNum
        {
            get
            {
                if (ViewState["PagNum"] != null)
                {
                    return Convert.ToInt32(ViewState["PagNum"]);
                }
                else
                {
                    return 0;
                }

            }
            set
            {
                ViewState["PagNum"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {

                    YouTubeVideos();
                }
            }
            catch (Exception ex)
            {

                ex.ToString();
            }
           

        }


        public void YouTubeVideos()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {

                    int cnt;


                    YouTubeVideoObject[] arr1 = YouTubeVideoHelper.GetVideos(ParentWebPart.youTubeChannelName, ParentWebPart.DeveloperKey);

                    DataTable dt = new DataTable();
                    dt.Columns.Add("Title");
                    dt.Columns.Add("VideoId");
                    dt.Columns.Add("VideoDuration");
                    dt.Columns.Add("PublishDate");
                    for (int i = 0; i < arr1.Length; i++)
                    {
                        DataRow row = dt.NewRow();
                        var VidObj = arr1[i];
                        row["Title"] = VidObj.Title;
                        row["VideoId"] = VidObj.VideoId;
                        row["VideoDuration"] = VidObj.VideoDuration;
                        row["PublishDate"] = VidObj.PublishDate;
                        dt.Rows.Add(row);
                    }

                 
                    PagedDataSource pagedata = new PagedDataSource();
                    DataTable dt2 = new DataTable();
                    DataSet ds = new DataSet();

                
                    ds.Tables.Add(dt);
                    cnt = ds.Tables[0].Rows.Count;
                    cnt = ds.Tables[0].Rows.Count;

                    pagedata.DataSource = ds.Tables[0].DefaultView;
                    pagedata.AllowPaging = true;
                    pagedata.PageSize = 10;
                    pagedata.CurrentPageIndex = PagNum;

                    int vcnt = cnt / pagedata.PageSize;
                    if (PagNum < 1)
                      
                        ImgBtnPrev.Visible = false;
                    else if (PagNum > 0)
                      
                        ImgBtnPrev.Visible = true;
                    if (PagNum == vcnt)
                        
                        ImgBtnNext.Visible = false;
                    if (PagNum < vcnt)
                      
                        ImgBtnNext.Visible = true;




                    VideosRepeater.DataSource = pagedata;
                    VideosRepeater.DataBind();





                  
                });
        
            }
            catch (Exception ex)
            {

                ex.ToString();
            }
            
        
        }


        protected void LnkBtnPrev(object sender, EventArgs e)
        {
            PagNum -= 1;
            YouTubeVideos();


        }
        protected void LnkBtnNext(object sender, EventArgs e)
        {
            PagNum += 1;
            YouTubeVideos();
        }

 

These are the Snaps.

YouTubeWP

 

WPProperties

once you deploy the solution just go and configure the webpart properties(if you have) put the channel name and developer Key(if you have) and save it. that’s it

 

Thanks and Have a good day

Happy SharePointing 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s