Filter the media items in the media library dialog using facet and tags

If you have a lot of media items in your Sitecore instance (thousands, ten of thousands), your content author would have difficulty in finding the media items that they want, thus making them frustrated trying do a simple task.

As a good developer you are, you try to help the content author by making their life easier. You start to think about using tags, by using tags the content author could filter the media items to a specific tags and refine their search from there.

So you sat down and make some notes of what you had in your mind

  • thousands of media items in the media library and the content author is having difficulties in filtering them
  • how about we apply tag(s) to the media item itself so that we can filter the media items based on those tags
  • from the content author perspective they would see a new section in the media library dialog to filter media items based on those tags
  • if by default when the media library opens it’s going to say 30,000 items, when the content author click on one of the tags it’s going to narrow down the result to 500 items and then the content author can further narrow it down by searching the file name

So you start of by buildling a POC, you downloaded https://marketplace.sitecore.net/Modules/Sitecore_Instance_Manager.aspx?sc_lang=en to help you quickly install/uninstall sitecore instances.

 

sim-install-new-instance

After the new Sitecore instance has been created, you start checking what the current media library dialog window looks like

medialibrary-dialog-original

You noticed that there’s three filter already built out-of-the-box which is Dimensions, Media type and Date uploaded. You want to create a new column in there called Tags, and you want to know how it work so you start by finding out how the Dimensions column works.

— you did your research and now ready to build your own custom filter based on tags

You’ve defined the following steps required to build the functionality

  1. Create a new class (MediaItemTagsIndexField) that extends AbstractComputedIndexField to extract the value of the assigned tags of each media items
  2. Create a new field and call it mediaitemtags in the index config file so that it would store the value returned by the MediaItemTagsIndexField
  3. Create a new Media Item Tags folder and create new tags item underneath it
  4. Update the image template in the master db both versioned and unversioned, add new field called Tags with Treelist as the media type and point the datasource to the Media Item Tags folder
  5. For POC purposes modify the default /sitecore/content/home item and add a new field with type image in it’s template definition
  6. Create a new class which extends the IFacetProvider which responsible for querying to the search provider and filter the results for you
  7. in the core db under the facets folder, create a new facet item called Tags and set the value of the fieldTypeName to the class that extends the IFacetProvider
  8. Test the functionality

 

MediaItemTagsIndexField.cs

 

zzz.RR.lucene.index.config

This is a patch file used to update the sitecore_marketing_asset_index_master index, we’re adding a new mediaitemtags field to the document

In the config file we’re targeting for the marketing_asset_index_master index however if some reason you did not use this file you could target the sitecore_master_index instead as Sitecore will fallback to use that index instead if marketing_asset_index_master is not available; or define it in the Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config file instead to apply it to all index files that refers to the default index configuration.

Create media item tags folder and tags item beneath it

content-tree

Update the image template to include the custom field to assign tags

add-tags-field-in-the-image-template

Do this for for this two Image templates and set the datasource to the media item tags folder

  • /sitecore/templates/System/Media/Unversioned/Image
  • /sitecore/templates/System/Media/Versioned/Image

Add a new field for the home item for the content author to be able to select an image

image-field-in-home

Now the content author can select an image for the home item, however we haven’t provide them with a way to filter the search results in the media library dialog yet. To do this first we need to create a new class that extends IFacetProvider that’s responsible to apply filtering on the search result.

MediaItemsTagFacetProvider.cs

Next we need to add a new column in the media library dialog UI so that the content author can use that to filter the media library search result. To do this switch to the core db and create a new item with facet template under /sitecore/client/Applications/Dialogs/SelectMediaDialog/PageSettings/Facets

tags-facet-item

Now switch back to the master db and click on the home item and click on the browse link

medialibrary-dialog-result

And now the content author has a new way to filter those media items based on the assigned tags!.

Here’s the files used in this post:

Hope this helps.

 

Leave a Reply

Your email address will not be published. Required fields are marked *