How to add a taxonomy through code

Topics: Customizing Orchard
Jul 12, 2011 at 4:34 PM


I'm trying to add a taxonomy to a part during the migration (I've not had any success with the import/export module). This is my code:


            var languageTaxonomy = _taxonomyService.GetTaxonomyByName("Language");
            if (languageTaxonomy == null) {
                languageTaxonomy = _orchardServices.ContentManager.New("Taxonomy").As<TaxonomyPart>();
                languageTaxonomy.Name = "Language";
                languageTaxonomy.ContentItem.As<RoutePart>().Title = "Language";
                foreach (var s in new[] { "English", "French", "German" }) {
                    var term = _taxonomyService.NewTerm(languageTaxonomy);
                    term.Name = s;
                    term.Selectable = true;

            var layer = _widgetsService.GetLayers().Where(r => r.Name == "Default").FirstOrDefault();
            var widget = _orchardServices.ContentManager.New("TaxonomyWidget").As<WidgetPart>();

            widget.LayerPart = layer;
            widget.Zone = _widgetsService.GetZones().Where(z => z == "Navigation").FirstOrDefault();
            widget.Position = "0";
            widget.Settings.Add("TaxonomyId", languageTaxonomy.Id.ToString());

            ContentDefinitionManager.AlterPartDefinition("ProductPart", cfg => cfg.
                                                                   WithField("Language", field => field
                                                                                                      .WithSetting("TaxonomyFieldSetting.TaxonomyId", languageTaxonomy.Id.ToString())
                                                                                                      .WithSetting("TaxonomyFieldSetting.LeavesOnly", "false")
                                                                                                      .WithSetting("TaxonomyFieldSetting.SingleChoice", "false")
                                                                                                      .WithSetting("FieldIndexing.Included", "true")



It's *almost* doing what I need it to do, but not quite.

It is creating the taxonomyField for the ProductPart and setting it to index, but it isn't setting the taxonomy. It is creating the widget and putting it in the right zone, but again, it's not assigning the taxonomy. 

languageTaxonomy is a TaxonomyPart and has an Id.

Any help appreciated as always :)

Jul 12, 2011 at 7:35 PM

First, have you tried to used the current default branch of the source code ? It might solve the import/export functionnality, and the whole question.

Sep 27, 2011 at 8:50 AM

probably a bit late now, but for the product part you just need to add an "s" to the end of the TaxonomyFieldSetting as shown below. I have not tried using the widget.

.WithSetting("TaxonomyFieldSettings.TaxonomyId", languageTaxonomy.Id.ToString()) .WithSetting("TaxonomyFieldSettings.LeavesOnly", "false") .WithSetting("TaxonomyFieldSettings.SingleChoice", "false")

Though the import/export may be a better way of doing it as that does work as well.