Querying images
This topic explains how to query instances of images. The examples below show you how to query all of the available images or how to query only a specific image by its ID.
NOTE: The code examples below work with the ID of the master version of the image and return the live version of the image. For more information about other scenarios, see Querying master and live versions in Content lifecycle.
Querying a single image
When querying the live version of a specific image 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 corresponding ID.
-
Get the live version.
The instance that corresponds to the ID argument is the master version of the image. You must get the live version explicitly.
NOTE: The live version is used only when displaying the news in a front-end scenario. If you want to manipulate the image, 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 an image by the ID of its master version.
Native API
private Image GetImageNativeAPI(Guid masterImageId)
{
LibrariesManager librariesManager = LibrariesManager.GetManager();
Image image = librariesManager.GetImages().Where(i => i.Id == masterImageId).FirstOrDefault();
if (image != null)
{
image = librariesManager.Lifecycle.GetLive(image) as Image;
}
return image;
}
First, you get an instance of the LibrariesManager class. You get the specified image by querying all images and filtering the collection by the ID property of the image. If the image 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 image:
Image image = librariesManager.GetImage(masterImageId);
Note that when the image does not exist, an exception of type ItemNotFoundException is thrown.
Fluent API
private Image GetImageFluentAPI(Guid masterImageId)
{
return App.WorkWith().Image(masterImageId).GetLive().Get();
}
First, you get the singular image facade of the master version with the specified ID. Then, to load the live version of the image, you call theGetLive method of the facade. Finally, to get the instance of the live version, you call the Get method.
Querying all images
When querying all images, you must perform the following:
-
Query all images.
First, get a query of all available images. The query includes all live, master and temp versions.
-
Filter the query.
Filter the query to return only the live versions.
-
Get the images.
Get the filtered images in a list.
-
Return the images.
Return the list of images.
The following code queries all published images.
Native API
private List<Image> GetAllImagesNativeAPI()
{
LibrariesManager librariesManager = LibrariesManager.GetManager();
return librariesManager.GetImages().Where(i => i.Status == ContentLifecycleStatus.Live).ToList();
}
First, you get an instance of the LibrariesManager class. You query all images, including the live, master and temp versions of each. You filter the collection to return only the live versions. Finally, you return the images as a list.
To get the images from a specific album, use the following code:
private List<Image> GetAllImagesByAlbumNativeAPI(Guid albumId)
{
LibrariesManager librariesManager = LibrariesManager.GetManager();
return librariesManager.GetImages().Where(i => i.Status == ContentLifecycleStatus.Live && i.Parent.Id == albumId).ToList();
}
In this code example you additionally filter the collection by the ID of the parent album.
Fluent API
private List<Image> GetAllImagesFluentAPI()
{
return App.WorkWith().Images().Where(b => b.Status == ContentLifecycleStatus.Live).Get().ToList();
}
First, you get an instance of the plural images facade. Then you get all the images, including the live, master and temp versions of each. You filter the collection to return only the live versions. Finally, you return the images as a list. If no images exist or there are not any published images, you return an empty list.
To get the images from a specific album, use the following code:
private List<Image> GetAllImagesByAlbumFluentAPI(Guid albumId)
{
return App.WorkWith().Album(albumId).Images().Where(i => i.Status == ContentLifecycleStatus.Live).Get().ToList();
}
In this code example you use the singular facade for the specified album and get the live versions of its images.
See Also