Patrick Lamber

Patrick Lamber - Mar 8, 2017 - Follow          
Technical consultant and Office Server and Services MVP.

Accessing videos with REST Video API through PowerShell

The post "Accessing videos with REST Video API through PowerShell" is a post of a post series. Find below all posts associated to this post series.
by Patrick Lamber on Mar 8, 2017
Filed under: Governance Office 365 Video Scripts Office 365
Share it:

The REST Video API is used to discover and interact with videos in the Office 365 Video service. You can find the official documentation here. This post is going to show you how you can access video data through the REST API.

The examples presented in this blog series will use the latest SharePoint Online SDK. You need to download the components to your machine. This post explains how to get the necessary DLLs.

Enumerate your videos in a channel

Videos can be enumerated in a channel with this script. Use the “Channels(‘guid’)/Videos” endpoint.

# your path to the net45 DLLs you downloaded

$pathToCSOM = "H:\Development\net45"
$tenantName = "yourTenantName"
$yourChannelId = "yourChannelId"
$videoPortalUrl = "https://$($tenantName).sharepoint.com/portals/hub/_api/VideoService/"

# Load the necessary SDK DLLs

[Reflection.Assembly]::LoadFrom("$($pathToCSOM)\Microsoft.SharePoint.Client.dll")
[Reflection.Assembly]::LoadFrom("$($pathToCSOM)\Microsoft.SharePoint.Client.Runtime.dll")

# Authenticate to SharePoint Online

$cred = Get-Credential
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password);

Function Get-RestContents ($url, $cred) {
    $r = [System.Net.WebRequest]::Create($url);
    $r.Credentials = $cred
    $r.Accept = "application/json;odata=verbose"
    $r.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")

    $res = $r.GetResponse()
    $stream = New-Object System.IO.StreamReader $res.GetResponseStream()
    $results = $stream.ReadToEnd()
    $stream.Dispose()
    return ($results | ConvertFrom-Json).d
}

$url = "$($videoPortalUrl)Channels(guid'$($yourChannelId)')/Videos"
$videoInfo = Get-RestContents $url $cred
$videoInfo.results | Select-Object Id, Title, ServerRelativeUrl, CreatedDate
You can obtain the list of the most recent playable videos on a channel with the endpoint "Channels(guid'$($yourChannelId)')/GetAllVideos". This endpoint filters out videos that aren't ready to play.

Get information about a specific video

The video enumeration provides you the video ID. This value can be used to obtain more information about a video.

# your path to the net45 DLLs you downloaded

$pathToCSOM = "H:\Development\net45"
$tenantName = "yourTenantName"
$yourChannelId = "yourChannelId"
$videoId = "yourVideoId"
$videoPortalUrl = "https://$($tenantName).sharepoint.com/portals/hub/_api/VideoService/"

# Load the necessary SDK DLLs

[Reflection.Assembly]::LoadFrom("$($pathToCSOM)\Microsoft.SharePoint.Client.dll")
[Reflection.Assembly]::LoadFrom("$($pathToCSOM)\Microsoft.SharePoint.Client.Runtime.dll")

# Authenticate to SharePoint Online

$cred = Get-Credential
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password);

Function Get-RestContents ($url, $cred) {
    $r = [System.Net.WebRequest]::Create($url);
    $r.Credentials = $cred
    $r.Accept = "application/json;odata=verbose"
    $r.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")

    $res = $r.GetResponse()
    $stream = New-Object System.IO.StreamReader $res.GetResponseStream()
    $results = $stream.ReadToEnd()
    $stream.Dispose()
    return ($results | ConvertFrom-Json).d
}

$url = "$($videoPortalUrl)Channels(guid'$($yourChannelId)')/Videos(guid'$($videoId)')"
$videoInfo = Get-RestContents $url $cred
$videoInfo.results | Select-Object Id, Title, ServerRelativeUrl, ViewCount, CreatedDate
The ViewCount will always return -1 with this endpoint. You need to use the Search API to get how many times a video has been viewed. An example is shown in the next snippet.

Get a count of how many times a video has been viewed

View counts of a video are returned only when retrieved by the Search endpoint. The reason for that is that view counts are aggregated by search analytics.

# your path to the net45 DLLs you downloaded

$pathToCSOM = "H:\Development\net45"
$tenantName = "yourTenantName"
$yourChannelId = "yourChannelId"
$videoId = "yourVideoId"
$videoPortalUrl = "https://$($tenantName).sharepoint.com/portals/hub/_api/VideoService/"

# Load the necessary SDK DLLs

[Reflection.Assembly]::LoadFrom("$($pathToCSOM)\Microsoft.SharePoint.Client.dll")
[Reflection.Assembly]::LoadFrom("$($pathToCSOM)\Microsoft.SharePoint.Client.Runtime.dll")

# Authenticate to SharePoint Online

$cred = Get-Credential
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password);

Function Get-RestContents ($url, $cred) {
    $r = [System.Net.WebRequest]::Create($url);
    $r.Credentials = $cred
    $r.Accept = "application/json;odata=verbose"
    $r.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")

    $res = $r.GetResponse()
    $stream = New-Object System.IO.StreamReader $res.GetResponseStream()
    $results = $stream.ReadToEnd()
    $stream.Dispose()
    return ($results | ConvertFrom-Json).d
}

$url = "$($videoPortalUrl)Channels(guid'$($yourChannelId)')/search/query(guid'$($videoId)')?$Select=ViewCount"
$viewCount = Get-RestContents $url $cred
$viewCount.results | Select-Object Id, Title, ServerRelativeUrl, ViewCount, CreatedDate
by Patrick Lamber on Mar 8, 2017
Filed under: Governance Office 365 Video Scripts Office 365
Share it:
The post "Accessing videos with REST Video API through PowerShell" is a post of a post series. Find below all posts associated to this post series.