How to filter content by multiple tags?

Posted by Community Admin on 04-Aug-2018 16:39

How to filter content by multiple tags?

All Replies

Posted by Community Admin on 07-May-2013 00:00

I have a dynamic content type (created in module builder) that I need to be able to filter across multiple tags (ie. the user can select one or more tags on a page and a list of content will filter according to the tag(s) that they have selected.)
What is the easiest way to accomplish this?

Posted by Community Admin on 10-May-2013 00:00

Hello,

The categories and tags widgets render the taxonomies in tree view control or as a list of categories/tags. The thing with selecting and filtering for multiple categories and tags is that the built in control allows single selection of an item in the tree/list. It does not save a set of selected categories based on which a query can be made to find all news items that have all selected categories.

To have this functionality a custom control have to be created (for example a list for multiple selection) utilizing the taxonomies API to render a list of all categories/tags and allow selection of a set of taxons based on which a query to be executed and filter the list of content items.

After this you can either create your own control to display the dynamic items (with repeater for example), or create a custom DynamicContentViewMaster class (it is responsible for displaying the list of dynamic items). Then override the InitializeControls method of the class like so:

protected override void InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
        
            int? totalCount = 0;
            this.DynamicContentListView.DataSource = GetMatchedResult(tagsCollection);
            this.DynamicContentListView.ItemDataBound += new EventHandler<RadListViewItemEventArgs>(this.DynamicContentListView_ItemDataBound);
  
            this.ConfigurePager(totalCount.Value);
  
            if (this.EnableSocialSharing == true)
                AddSocialSharing();
        

where tagsCollection are the selected from the list control tags placed in an array. Here is the sample GetMatchedResult() method:
public List<DynamicContent> GetMatchedResult(string[] taxonomies)
        
            var providerName = String.Empty;
            DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
            Type testType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.ModuleTest.Test");
  
            var matchedData = new List<DynamicContent>();          
            var dynamicItemsAll = dynamicModuleManager.GetDataItems(testType).Where(i=>i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
            var number = taxonomies.Count();
  
            foreach (DynamicContent itm in dynamicItemsAll)
            
                int counter = 0;
                for (int i = 0; i < number; i++)
                
                    var tagsDesc = TypeDescriptor.GetProperties(itm).Find("Tags", true);
                    if (tagsDesc != null)
                    
                        // need to implement a check if tagsDesc contains taxonomies[i] and if so increment the counter
                        counter++;
                    
                    else
                    
                        break;
                    
                
                if (counter == number)
                
                    matchedData.Add(itm);
                
            
            return matchedData;
        
Now in order to be able to use the customDynamicContentViewMaster class you need to also override the DynamicContentView control (the whole widget) and register your custom control in its initialization. I have attached 2 custom files to this reply. Please place them in your solution and Build. Then register the CustomDynamicContentView as a custom control in the toolboxes (easiest way is to use Thunder for this). Now when you drop the custom control it will be the same as the default one (created by the module builder) but it will use the CustomDynamicContentViewMaster class.

All the best,
Stanislav Velikov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed