This project is read-only.

How to programmatically set a field

Topics: Customizing Orchard
Jul 10, 2011 at 12:14 AM


Me again :) I'm now trying to write an import module. One property is mapped to a taxonomy field. How do I go about changing the value of the field? 

I've eventually found the field from ContentItem.As<ContentPart>.Fields.Where(f => f.Name == "Language").FirstOrDefault but I can't seem to work out how to modify it. 

All help appreciated!

Jul 10, 2011 at 5:04 AM

For the specific case of Taxonomy, you just have to change the TermContentItemRecord corresponding to this field. In your class, just take a dependency on IRepository<TermContentItenRecord> and modify what you need. You can also use the ITaxonomyService interface to call UpdateTerms, which removes and adds terms for a specific field name. Might be easier too.

Jul 10, 2011 at 10:44 PM

Works a charm :) Thanks

I need to be more vigilant for things like ITaxonomyService....

Jul 11, 2011 at 12:44 AM


I'm having another related issue. Not sure if it's worth a new topic.

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. 

Is it something that I'm doing wrong here? I'm not 100% sure what IOrchardServices.ContentManager does. Do I need to publish each item? Or just create it? And what am I doing wrong to stop the correct taxonomy from being set?


Oct 11, 2011 at 5:14 PM

where I have to write this code to create new taxonomy

Aug 9, 2012 at 10:23 PM

I understand it is old post but anybody managed to assign a taxonomy in the code?

I have the similat problem with no answer unfortunately.

Aug 17, 2012 at 8:42 PM
Edited Aug 17, 2012 at 8:43 PM


What you probably need to do is cast the widget variable to TermWidgetPart, which has a property called TaxonomyPartRecord. Setting this property to languageTaxonomy.Record should do the trick.


Aug 17, 2012 at 8:44 PM
arpitaj wrote:

where I have to write this code to create new taxonomy

That depends on when you want to execute that code. BAsically you can write that code in any class that makes sense for your scenario.