Querying videos
This topic explains how to query instances of videos. The examples below show you how to query all of the available videos or how to query only a specific video by its ID.
NOTE: The code examples below work with the ID of the master version of the video and return the live version of the video. For more information about other scenarios, see Querying master and live versions.
Querying a single video
When querying the live version of a specific video by the ID of its master version, you must perform the following:
-
Get the master version.
First, get an instance of the master version with the specified ID.
-
Get the live version.
The instance that corresponds to the ID argument is the master version of the video. You must get the live version explicitly.
NOTE: The live version is used only when displaying the video in a front-end scenario. If you want to manipulate the video, consider using the master version. To transfer the changes to the live version, publish the master version.
-
Return the live version.
Return the live version.
The following code queries the live version of a video by the ID of its master version.
Native API
private Video GetVideoNativeAPI(Guid masterVideoId)
{
LibrariesManager librariesManager = LibrariesManager.GetManager();
Video video = librariesManager.GetVideos().Where(d => d.Id == masterVideoId).FirstOrDefault();
if (video != null)
{
video = librariesManager.Lifecycle.GetLive(video) as Video;
}
return video;
}
First, you get an instance of the LibrariesManager class. You get the specified video by querying all videos and filtering the collection by theID property of the video. If the video exists, you get its live version. If no live version exists (i.e. the item has not been published), you return null. Finally, you return the live version.
You can also use the following code to retrieve the video:
Video video = librariesManager.GetVideo(masterVideoId);
Note that when the video does not exist, an exception of type ItemNotFoundException is thrown.
Fluent API
private Video GetVideoFluentAPI(Guid masterVideoId)
{
return App.WorkWith().Video(masterVideoId).GetLive().Get();
}
First, you get the singular video facade of the master version with the specified ID. Then, to load the live version of the video, you call theGetLive method of the facade. Finally, to get the instance of the live version, you call the Get method.
Querying all videos
When querying all videos, you must perform the following:
-
Query all videos.
First, get a query of all available videos. The query includes all live, master and temp versions.
-
Filter the query.
Filter the query to return only the live versions.
-
Get the videos.
Get the filtered videos in a list.
-
Return the videos.
Return the list of videos.
The following code queries all published videos.
Native API
private List<Video> GetAllVideosNativeAPI()
{
LibrariesManager librariesManager = LibrariesManager.GetManager();
return librariesManager.GetVideos().Where(d => d.Status == ContentLifecycleStatus.Live).ToList();
}
First, you get an instance of the LibrariesManager class. You query all videos, including the live, master and temp versions of each. You filter the collection to return only the live versions. Finally, you return the videos as a list.
To get the videos from a specific video library, use the following code:
private List<Video> GetAllVideosByVideoLibraryNativeAPI(Guid videoLibraryId)
{
LibrariesManager librariesManager = LibrariesManager.GetManager();
return librariesManager.GetVideos().Where(d => d.Status == ContentLifecycleStatus.Live && d.Parent.Id == videoLibraryId).ToList();
}
In this code example you additionally filter the collection by the ID of the parent video library.
Fluent API
private List<Video> GetAllVideosFluentAPI()
{
return App.WorkWith().Videos().Where(d => d.Status == ContentLifecycleStatus.Live).Get().ToList();
}
First, you get an instance of the plural videos facade. Then you get all the videos, including the live, master and temp versions of each. You filter the collection to return only the live versions. Finally, you return the videos as a list. If no videos exist or there are not any published videos, you return an empty list.
To get the videos from a specific video library, use the following code:
private List<Video> GetAllVideosByVideoLibraryFluentAPI(Guid videoLibraryId)
{
return App.WorkWith().VideoLibrary(videoLibraryId).Videos().Where(d => d.Status == ContentLifecycleStatus.Live).Get().ToList();
}
In this code example you use the singular facade for the specified video library and get the live versions of its videos.