Filter Gallery in Canvas app dynamically on CDS entity view(s)

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com

  • Do you want to filter your Data source in Canvas app based on entity views in CDS?
  • Do you want to filter your Gallery control dynamically based on CDS views?

If you are having these questions, you are on the right page. In this blog I am going to explain on how we can dynamically filter a CDS data source based on entity views. This blog is rather going to be short unlike my usual blogs. But they it can be pretty useful based stuff to know.

Below is my gallery control which show a list of accounts. By default, it it showing all accounts in the CDS database.

Filter Gallery based on CDS entity views

What we need to do is filter the gallery control based on the views selected dynamically. You may be wondering – “Can’t I already do that?” The answer is YES. Galleries infact have the option to show data based on specific views of a CDS entity.

Filter Gallery based on CDS entity views

But there is slight difference between what is there and what we are trying to achieve. The above screenshot display what you can achieve at design time. However the requirement here is to filter the gallery at runtime.

But wait. This design time configuration can help you a lot. Select “My Active Account” or any other view you want and check for the formula Items in the Gallery.

Filter Gallery based on CDS entity views

Wonderful isn’t it? Now all you need is to change the filter dynamically on select of the individual buttons. But wait the things are not so direct when you are working in Canvas app. To achieve this, I use the concept of context variables. Below is the code “OnSelect” of Active Accounts button.

Filter Gallery based on CDS entity views

I am using a context variable – “ViewFilter” and setting it’s value to the selected view. Once I have done this, I will change the formula of the Items of Gallery as well to use this ViewFilter variable. Below is the formula for Items collection of Gallery.

Filter Gallery based on CDS entity views

That’s easy. And the more you work in canvas app, you kind of get hold on how to work out the various functionalities here.

You will also like the below posts.

Hope this helps!
Debajit Dutta
(Business Solutions MVP)