DFO 4.0.0 - 5 Querying Assets

Sometimes you, as a developer, might want to do more direct queries against the assets in Digizuite DAM. Maybe you want to:

  • Offer a listing of product white papers available for download that updates dynamically

  • Show an archive of videos for the current target audience

  • Give the end user access to direct free text search in certain assets

  • Have a gallery or a slider that automatically selects images marked in your DAM for that purpose by your image editor.

In cases such as these you might want to consider doing a custom query for assets. Luckily the Optimizely/Digizuite integration is prepared for this and supports it fully.





Querying

In order to search, you basically have to construct an AssetQueryParameters object.

This will specify all the important parameters of your search. These are some of the parameters you can set:

  • FreeText (string). A free text search query. By default this will do a free text search against the title, description, keywords and note field - but it can of course be configured.

  • Facets (List<FacetRequest>). Any additional fields to filter for.

  • Limit (int). How many assets to return.

  • QueryCacheDuraction (TimeSpan). The timespan to cache the results for. This is very important in order to ensure decent performance on your site and DAM Center. This can also be set through the method CacheFor.

When you have constructed a complete AssetQueryParameters object, you can use it in the DigizuiteClient.Search(AssetQueryParametres) method. This will return an IEnumerable<IContent> that in turn can be cast to whatever type you expect.

If you require the generated IContent objects to have a different 'Parent' property than the default (which is the Digizuite Content Providers entry point), you can specify it in an optional parameter. This can be useful if you want the objects to be listed below a certain folder for example.

Selection factories

To make it even easier to use the values from the DAM Center in custom property drop down lists - for example in a block to hold a dynamic gallery, the integration also provides SelectionFactory for folders in the Digizuite.Episerver.Helpers namespace, ready to use in attributes on properties like this: 

[SelectMany(SelectionFactoryType = typeof(FoldersSelectionFactory))]
public virtual string Folders { get; set; }

See a full example further down!

Example: Building a Gallery Block

Putting it all together, let's create a gallery block that shows a dynamic gallery of images from Digizuite, based on editorial preferences.

First, we'll create the block definition. 

We will also make it optional which folders to search in, a Free text query, how many assets to show and so on. However, we won't worry about paging for now.



[ContentType(DisplayName = "Gallery", GUID = "b05d16f0-877d-4784-8bf1-4a0ee2e57fd1", Description = "Shows a gallery of DAM assets")] public class GalleryBlock : BlockData { [CultureSpecific] [Display( Name = "Headline", Description = "Headline", GroupName = SystemTabNames.Content, Order = 1)] public virtual string Headline { get; set; } [Display( Name = "Max number of assets", Description = "", GroupName = SystemTabNames.Content, Order = 10)] public virtual int MaxNumberOfAssets { get; set; } [Display( Name = "Free Text", Description = "", GroupName = SystemTabNames.Content, Order = 30)] public virtual string FreeText { get; set; } [Display( Name = "Folders", Description = "", GroupName = SystemTabNames.Content, Order = 40)] [SelectMany(SelectionFactoryType = typeof(FoldersSelectionFactory))] public virtual string Folders { get; set; } public override void SetDefaultValues(ContentType contentType) { base.SetDefaultValues(contentType); this.MaxNumberOfAssets = 20; } }

When we display this to the editors in edit-mode, this is what they'll see:

We also have to have a standard controller that will popular a View Model with the assets to show. We do that here:

protected override IViewComponentResult InvokeComponent(GalleryBlock currentContent) { GalleryBlockViewModel gvm = new GalleryBlockViewModel(); gvm.CurrentBlock = currentContent; AssetQueryParameters aqm = new AssetQueryParameters(); aqm.Limit = currentContent.MaxNumberOfAssets; var facets = new List<FacetRequest>() { new FacetRequest() { FacetMode = FacetMode.QueryOnly, Recursive = false, SearchKey = "assetType", Values = Enums.AssetType.Image.ToString().MakeIntList() } }; if (!string.IsNullOrEmpty(currentContent.Folders)) { facets.Add(new FacetRequest() { FacetMode = FacetMode.QueryOnly, Recursive = false, SearchKey = _client.Configuration.FolderMetafieldGuid.ToString(), Values = currentContent.Folders.Split(",").ToList() }); } aqm.Facets = facets; if (!string.IsNullOrEmpty(currentContent.FreeText)) aqm.FreeText = currentContent.FreeText; gvm.Images = _client.Search(aqm.CacheFor(TimeSpan.FromMinutes(1)))?.Cast<IDigizuiteImage>()?.ToList(); return View(gvm); }

Notice how we also make sure to set a cache timeout of 1 minute when doing the searching? That ensures a pretty updated image list, while not cause load to the DAM server for every page view. We could probably safely increase this number a lot. 

We also ensure that we will only search for images. AssetTypes.Image. The view is basically just about rendering the gallery, with the addition of rendering the images in the selected media format as described here: DFO 4.0.0 - 4 Referencing and Rendering Assets. Here is what the end result could look like: