Blog Categories only in dropdown list

Posted by Community Admin on 03-Aug-2018 06:43

Blog Categories only in dropdown list

All Replies

Posted by Community Admin on 03-Nov-2011 00:00
I've bee able to build a custom dropdown list control based on this thread:
http://www.sitefinity.com/devnet/forums/sitefinity-4-x/general-discussions/categorytree.aspx

However, I want to take it one step further and ONLY show the categories that are used in blog posts.

How could I expand upon the post above to accomplish this?

Thank you.
Posted by Community Admin on 04-Nov-2011 00:00
Hi Amir,

Can you attach the control that you have build so we can have a look at it and see whats the proper way to implement the required code?

Kind regards,
Victor Velev
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
Posted by Community Admin on 09-Nov-2011 00:00
Thanks Victor,

Here is the current code:

protected void Page_Load(object sender, EventArgs e)
        
            var tm = TaxonomyManager.GetManager();
            var tax = tm.GetTaxonomy(TaxonomyManager.CategoriesTaxonomyId);
            var taxa = tax.Taxa;
 
            categorylist.Items.Add(new ListItem("Categories", "0"));
            foreach (HierarchicalTaxon taxon in taxa)
            
                string url = "";
                var evaluator = new TaxonomyEvaluator();
                var taxonBuildOptions = TaxonBuildOptions.None;
                if (tax is HierarchicalTaxonomy)
                    taxonBuildOptions = TaxonBuildOptions.Hierarchical;
                else if (tax is FlatTaxonomy)
                    taxonBuildOptions = TaxonBuildOptions.Flat;
                string evaluatedResult = "/blog" + evaluator.BuildUrl(tax.Name +"/", taxon.UrlName, "Category", taxonBuildOptions, UrlEvaluationMode.UrlPath, null);
                var li = new ListItem(taxon.Title, evaluatedResult);
                categorylist.Items.Add(li);
            
 
 
 
 
        
Where "categoryList" is a simple DropDownList. 

So, this gets ALL of the categories in the system and binds them to a dropdownlist, but as i mentioned, I'd like to ONLY show the categories that were assigned to blog posts.  Thank you.

Posted by Community Admin on 11-Nov-2011 00:00
Hello Amir,

Thank you for providing the code. We will need some more time to explore the code and provide you with a solution.

Best wishes,
Victor Velev
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
Posted by Community Admin on 15-Nov-2011 00:00
Hi Amir,

We have prepared a sample code for you. Feel free to modify it so it suits your needs:

var catList = new List<ITaxon>();
         var blogPosts = App.WorkWith().BlogPosts().Get().ToList();
         if (blogPosts != null)
         
             
             var manager = TaxonomyManager.GetManager();
 
             foreach (var blog in blogPosts)
             
                 var blogsCats = blog.GetValue<TrackedList<Guid>>("Category");
                 if (blogsCats != null)
                 
                     foreach (var cat in blogsCats)
                     
                         var category = manager.GetTaxon(cat);
                         if (category != null && !catList.Contains(category))
                         
                             catList.Add(category);
 
                         
                     
                 
             
         
         foreach (var c in catList)
         
             string url = "";
             var evaluator = new TaxonomyEvaluator();
 
             var taxonBuildOptions = TaxonBuildOptions.None;
             if (c is HierarchicalTaxonomy)
                 taxonBuildOptions = TaxonBuildOptions.Hierarchical;
 
             else if (c is FlatTaxonomy)
                 taxonBuildOptions = TaxonBuildOptions.Flat;
 
             string evaluatedResult = "/blog" + evaluator.BuildUrl(c.Name + "/", c.UrlName, "Category", taxonBuildOptions, UrlEvaluationMode.UrlPath, null);
 
             categorylist.Items.Add(c.Title);
 
         


Best wishes,
Victor Velev
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