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 For developers: Query master and live versions in For developers: Content lifecycle.
When querying the live version of a specific image by the ID of its master version, you must perform the following:
The following code queries the live version of an image by the ID of its master version.
Image GetImageNativeAPI(Guid masterImageId)
LibrariesManager librariesManager = LibrariesManager.GetManager();
Image image = librariesManager.GetImages().Where(i => i.Id == masterImageId).FirstOrDefault();
image = librariesManager.Lifecycle.GetLive(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 the namespace of the Image is Telerik.Sitefinity.Libraries.Model.Image. Also when the image does not exist, an exception of type ItemNotFoundException is thrown.
private Image GetImageFluentAPI(Guid masterImageId)
Image GetImageFluentAPI(Guid masterImageId)
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 the GetLive method of the facade. Finally, to get the instance of the live version, you call the Get method.
To query images by an album, you must perform the following:
NOTE: Another way of getting the images from a specific album, is to query all the images and filter the collection by the ID of the album. You can find an example of this approach in the next section.
Here is a code example:
IQueryable<Image> GetImagesByAlbumNativeAPI(Guid albumId)
Album album = librariesManager.GetAlbum(albumId);
album.Images().Where(i => i.Status == ContentLifecycleStatus.Live);
IQueryable<Image> GetImagesByAlbumFluentAPI(Guid albumId)
App.WorkWith().Album(albumId).Images().Where(d => d.Status == ContentLifecycleStatus.Live).Get();
When querying all images, you must perform the following:
The following code queries all published images.
NOTE: You can filter the query to get a specific set of images. For example, you can get the images from a specific album:
List<Image> GetAllImagesByAlbumNativeAPI(Guid albumId)
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.
NOTE: You can filter the query to get a specific set of images (as well as deleted broken images). For example, you can get the images from a specific album:
List<Image> GetAllImagesByAlbumFluentAPI(Guid albumId)
App.WorkWith().Images().Where(i => i.Album.Id == albumId && i.Status == ContentLifecycleStatus.Live).Get().ToList();
In this code example you use the plural facade for the images and filter them by the ID of the specified album. Then, you get the live versions of the selected images.
Back To Top