# Query 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*.

> info **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](https://www.progress.com/documentation/sitefinity-cms/for-developers-query-master-and-live-versions) in [For developers: Content lifecycle](https://www.progress.com/documentation/sitefinity-cms/for-developers-content-lifecycle).

## Query a single image

When querying the live version of a specific image by the *ID* of its master version, you must perform the following:

1. Get the master version.\
   First, get an instance of the master version with the corresponding ID.
2. 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.

   > info  **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.
3. Return the live version.

The following code queries the live version of an image by the ID of its master version.

**Native API**

```C#
using System;
using System.Linq;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;

namespace SitefinityWebApp
{
   public class QuerySingleImageNativeAPI
   {
       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:

```C#
using System;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;

namespace SitefinityWebApp
{
   public class QuerySingleImageNativeAPI2
   {
       private Image GetImageNativeAPI(Guid masterImageId)
       {
           LibrariesManager librariesManager = LibrariesManager.GetManager();
           Image image = librariesManager.GetImage(masterImageId);
          
           return image;
       }
   }
}
```

> info **NOTE**: When the image does not exist, an exception of type `ItemNotFoundException` is thrown.

**Fluent API**

```C#
using System;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Libraries.Model;

namespace SitefinityWebApp
{
   public class QuerySingleImageFluentAPI
   {
       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 the `GetLive` method of the facade. Finally, to get the instance of the live version, you call the `Get` method.

## Query images by an album

To query images by an album, you must perform the following:

1. Get the libraries manager.\
   Get an instance of the `LibrariesManager` object.
2. Get the album.\
   Get an instance of the specified album. For more information, see [For developers: Query image libraries](https://www.progress.com/documentation/sitefinity-cms/for-developers-query-image-libraries).
3. Get the images.\
   Get the images by calling the `Images` extension method of the `Album` instance. This extension method is located in the `Telerik.Sitefinity.Modules.Libraries` namespace.

> info **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.

Use the following code samples:

**Native API**

```C#
using System;
using System.Linq;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;

namespace SitefinityWebApp
{
   public class QueryImageByAlbumNativeAPI
   {
       public static IQueryable<Image> GetImagesByAlbumNativeAPI(Guid albumId)
       {
           LibrariesManager librariesManager = LibrariesManager.GetManager();

           Album album = librariesManager.GetAlbum(albumId);

           return album.Images().Where(i => i.Status == ContentLifecycleStatus.Live);
       }
   }
}
```

**Fluent API**

```C#
using System;
using System.Linq;
using Telerik.Sitefinity;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Libraries.Model;

namespace SitefinityWebApp
{
   public class QueryImageByAlbumFluentAPI
   {
       public static IQueryable<Image> GetImagesByAlbumFluentAPI(Guid albumId)
       {
           return App.WorkWith().Album(albumId).Images().Where(d => d.Status == ContentLifecycleStatus.Live).Get();
       }
   }
}
```

## Query all images

When querying all images, you must perform the following:

1. Query all images.\
   First, get a query of all available images. The query includes all live, master and temp versions.
2. Filter the query.\
   Filter the query to return only the live versions.
3. Get the images.\
   Get the filtered images in a list.
4. Return the images.\
   Return the list of images.

> info **NOTE:** You can filter the query to get a specific set of images. For example, you can get the images from a specific album.

The following code queries all published images:

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;

namespace SitefinityWebApp
{
   public class QueryAllImagesNativeAPI
   {
       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.

> info **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:

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using Telerik.Sitefinity;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Libraries.Model;

namespace SitefinityWebApp
{
   public class QueryAllImagesFluentAPI
   {
       public static List<Image> GetAllImagesByAlbumFluentAPI(Guid albumId)
       {
           return 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.
