Creating a tree of Taxonomies In Code

Topics: Troubleshooting, Writing modules
Mar 3, 2013 at 5:46 AM
Edited Mar 3, 2013 at 5:46 AM
I attempting to create a simple tree of taxonomies via IFeatureEventHandler, basically default set of items.
  1. sub items appear to not be added to the correct parent
  2. Items that are added don't seem to work correctly with the UI, i.e. I cant in the admin add children to the items that were created via code or sub items don't add to the correct parent.
 if (this.taxonomyService.GetTaxonomyByName("Project States") == null)
            {
                var projectStates = this.Services.ContentManager.New<TaxonomyPart>("Taxonomy");
                projectStates.Name = "Project States";
                this.Services.ContentManager.Create(projectStates, VersionOptions.Published);

                this.CreateTerm(projectStates, "Planning");
                this.CreateTerm(projectStates, "In Progress");
                var completed = this.CreateTerm(projectStates, "Completed");
                var sold = this.CreateTerm(projectStates, "Sold");
                //this.CreateTerm(projectStates, "Pending");
                //this.CreateTerm(projectStates, "For Sale");

                this.taxonomyService.MoveTerm(projectStates, sold, completed);

            }

private TermPart CreateTerm(TaxonomyPart taxonomy, string name)
        {
            var term = this.taxonomyService.NewTerm(taxonomy);
            term.Name = name;
            this.taxonomyService.ProcessPath(term);
            this.Services.ContentManager.Create(term, VersionOptions.Published);

            return term;
        }
Coordinator
Mar 3, 2013 at 6:17 AM
Look how the taxonomy admin controller is doing it, and do the same.
Mar 3, 2013 at 7:19 AM
I followed that code you suggested but still getting the same issue, it appears that they are actually linked correctly in the db (using path to verify that). It has to do with the order that the terms are being returned. I still digging but when this code it run the order is coming back incorrect and that is throwing off the view.
        public IEnumerable<TermPart> GetTerms(int taxonomyId) {
            return _contentManager.Query<TermPart, TermPartRecord>()
                .Where(x => x.TaxonomyId == taxonomyId)
                .WithQueryHints(new QueryHints().ExpandRecords<AutoroutePartRecord, TitlePartRecord>())
                .List()
                .OrderBy(t => t);
        }
This is in TaxonomyService.cs, it has an orderby line but I not the best at linq and not totally clear on it's ramifications or why it works when I manually create terms but fails when I us API to create them.
Mar 3, 2013 at 4:41 PM
Not sure why but if I create in alphabetical per level + in order of tree structure the code works w/o issue ,and all of the issue I was having are gone.
I think there is a case where the order from the code I listed in the last post can return incorrectly if Items are added in a particular order, but I was never able to get the to bottom of why.

Anyways new create code:
if (this.taxonomyService.GetTaxonomyByName("Project States") == null)
            {
                var projectStates = this.Services.ContentManager.New<TaxonomyPart>("Taxonomy");
                projectStates.Name = "Project States";
                this.Services.ContentManager.Create(projectStates, VersionOptions.Published);

                var completed = this.CreateTerm(projectStates, "Completed");
                this.CreateTerm(projectStates, "For Sale", completed);
                this.CreateTerm(projectStates, "Pending", completed);
                this.CreateTerm(projectStates, "Sold", completed);
                this.CreateTerm(projectStates, "In Progress");
                this.CreateTerm(projectStates, "Planning");

            }
Create Term Code (Slug generation still needs work) :
 private TermPart CreateTerm(TaxonomyPart taxonomy, string name, TermPart parent = null)
        {
            var term = this.taxonomyService.NewTerm(taxonomy);
            term.Container = parent == null ? taxonomy.ContentItem : parent.ContentItem;
            term.Name = name;
            term.Slug = parent == null ? string.Concat(taxonomy.Slug, "/", term.Name).ToLower().Replace(" ", "-") : string.Concat(taxonomy.Slug, "/", parent.Name, "/", term.Name).ToLower().Replace(" ", "-");

            this.taxonomyService.ProcessPath(term);
            this.Services.ContentManager.Create(term, VersionOptions.Published);

            return term;
        }