More in this section

Forums / General Discussions / Blog Categories only in dropdown list

Blog Categories only in dropdown list

5 posts, 0 answered
  1. Amir
    Amir avatar
    33 posts
    Registered:
    24 Dec 2010
    03 Nov 2011
    Link to this post
    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.
  2. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    04 Nov 2011
    Link to this post
    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
  3. Amir
    Amir avatar
    33 posts
    Registered:
    24 Dec 2010
    09 Nov 2011
    Link to this post
    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.

  4. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    11 Nov 2011
    Link to this post
    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
  5. Victor Velev
    Victor Velev avatar
    1345 posts
    Registered:
    06 Jul 2015
    15 Nov 2011
    Link to this post
    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
5 posts, 0 answered