Thursday, November 21, 2013

Create Embedded Video / External Link Video using managed CSOM

We have been introduced to two new ways of adding videos to SharePoint in the latest version of SP i.e. SP 2013:
1. Embedded videos
2. Link to video.
Following is the code to add these videos programmatically using CSOM(client side object model).


using Microsoft.SharePoint.Client;

using Microsoft.SharePoint.Client.Video;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Upload_Videos
{
  class Program
  {
     static void Main(string[] args)
     {
        string _siteUrl = ""; // url of site
        string _videoTitle = ""; // title of video
        string _embeddedCode = ""; // embedded code for video
        string _externalLink = ""; // link for video
        using (ClientContext context = new ClientContext(_siteUrl))
        {
           try
           {
               Web _web = context.Web; // gets web object of current context
               List _videoList = _web.Lists.GetByTitle("Assets"); // gets object of library where we need to add video , make sure that this library has option to add video content type
               context.Load(_videoList.RootFolder); 
               context.ExecuteQuery();
               var _contentType = GetContentType(context, _videoList, "Video");// gets content type ID for video 
               var _contentTypeId = _contentType.Id;
               VideoSet.CreateVideo(context, _videoList.RootFolder, _videoTitle, _contentTypeId); // creates video with the name stored in _videoTitle
               context.ExecuteQuery();
               Microsoft.SharePoint.Client.Folder Ofile = context.Web.GetFolderByServerRelativeUrl(_videoList.RootFolder.ServerRelativeUrl + "/" + _videoTitle);
               context.Load(Ofile.ListItemAllFields);
               context.ExecuteQuery();

               Ofile.ListItemAllFields["Title"] = _videoTitle;
               Ofile.ListItemAllFields["VideoSetEmbedCode"] =_embeddedCode;
               //Ofile.ListItemAllFields["VideoSetExternalLink"] = _externalLink; //to set link for video
               Ofile.ListItemAllFields.Update();
               context.ExecuteQuery();

               Console.WriteLine("Video created!");
           }

           catch (Exception ex)
           {
              Console.WriteLine(ex.ToString());
           }
           Console.ReadLine();
        }
   }
   static private ContentType GetContentType(ClientContext ctx, List lst, string contentType)
   {
       ContentTypeCollection listContentTypes = lst.ContentTypes;
       ctx.Load(listContentTypes, types => types.Include(type => type.Id, type => type.Name,type => type.Parent));
       var result = ctx.LoadQuery(listContentTypes.Where(c => c.Name == contentType));
       ctx.ExecuteQuery();

       ContentType targetContentType = result.FirstOrDefault(); 
       return targetContentType;
   }
  }
}

Share This!

5 comments:

  1. You have to add Microsoft.SharePoint.Client.Video dll into your references folder. Please let me know if it works. Thanks!

    ReplyDelete
  2. For the Microsoft.SharePoint.Client.Video namespace, I found that the assembly I needed was Microsoft.SharePoint.Client.DocumentManagement (though I believe it's also included in a few other assemblies).

    Makes sense, given that VideoSet inherits from DocumentSet.

    ReplyDelete
  3. Thanks I was looking for this badly!!!

    ReplyDelete
  4. Thanks I was looking for this badly!!!

    ReplyDelete