Finding media

To find media, the easiest way is to use the Query method. You pass the MediaItem type (or any subclass) as type argument and then add conditions/filters that limits the query.

SearchFor

The SearchFor criteria only works for MediaItems or subclasses of, but it will perform a search in meta data, categories and name of the item.

var animalMedia = client.Query<MediaItem>()
    .SearchFor("animal")
    .ToList();

Where

The Where clause allows us to add specific filters for our query.

By Id

We can filter out a specific MediaItem by it's Id.

var mediaItem = client.Query<MediaItem>()
    .Where(m => m.Id == 384)
    .FirstOrDefault();

By Vault

We can also filter out media from a specific vault by filtering the vaultId property.

var itemsInVault1 = client.Query<MediaItem>()
    .Where(m => m.VaultId == 30)
    .ToList();

You can also specify multiple vaults to search in by using contains.

var vaultsToSearchIn = new[] {1, 2};
var itemsInVault1And2 = client.Query<MediaItem>()
    .Where(m => vaultsToSearchIn.Contains(m.VaultId))
    .ToList();

By category

If we want to filter by category we use the Contains/ContainsAll method of the Categories property

var itemsInCategory2 = client.Query<MediaItem>()
    .Where(m => m.Categories.Contains(2))
    .ToList();
var itemsInCategory1or2 = client.Query<MediaItem>()
    .Where(m => m.Categories.ContainsAll(1, 2))
    .ToList();

In state

Normally we only filter media that is ready to use. When media is uploaded it has to be organized before it is ready to use (see uploading section for more information). To filter out those files that has not been organized, we need to filter by the state of the item. The state is not a property on the MediaItem so it has to be filtered in some other way. There exists a Where method that takes a string that allows us to send custom filter commands, for example to filter by state.

var analyzedMedia = client.Query<MediaItem>()
    .Where("MediaItemState:" + MediaItemStates.MetadataAnalyzed)
    .ToList();
var organizedMedia = client.Query<MediaItem>()
    .Where("MediaItemState:" + MediaItemStates.MediaReadyToUse)
    .ToList();
var nonOrganizedMedia = client.Query<MediaItem>()
    .Where("MediaItemState:" + MediaItemStates.AnalyzedAndStored)
    .ToList();

Take

The take option allows us to limit the number of results that is returned. The following example returns 10 items from the result.

var firstTenItemsInVault2 = client.Query<MediaItem>()
    .Where(m => m.VaultId == 30)
    .Take(10).ToList();

Skip

To paginate the result we can add the Skip method that skips a number of results. The following example returns item 11 to 20 from the result.

var elevenToTwentyItemsInVault2 = client.Query<MediaItem>()
    .Where(m => m.VaultId == 30)
    .Skip(10)
    .Take(10)
    .ToList();

Include

Normally only a smaller subset of all properties of the MediaItem is populated. Access, Categories and PublishInformation are excluded by default to increase performance of the query. To include these use the Include method.

var mediaItemsWithCategoriesAndPublishInformation = client.Query<MediaItem>()
    .Include(m => m.Access)
    .Include(m => m.PublishInformation)
    .Include(m => m.Categories)
    .Take(100)
    .ToList();

OrderBy

To order the result use the OrderBy and OrderByDescending methods. You can order by any property but only some are supported for server side sorting. If you use a property that is not supported for server side sorting and combine these with Take and/or Skip, an error will occur since the output will not be correct.

The supported server side sort properties are DateAdded, Id, Name.

var oldestMedia = client.Query<MediaItem>()
    .OrderBy(m => m.DateAdded)
    .Take(10);
var latestMedia = client.Query<MediaItem>()
    .OrderByDescending(m => m.DateAdded)
    .Take(10);
comments powered by Disqus