How to programmatically set a field

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

Hi

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!

Coordinator
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

Hi

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";
                _taxonomyService.CreateTermContentType(languageTaxonomy);
                _orchardServices.ContentManager.Create(languageTaxonomy);
                foreach (var s in new[] { "English", "French", "German" }) {
                    var term = _taxonomyService.NewTerm(languageTaxonomy);
                    term.Name = s;
                    term.Selectable = true;
                    _orchardServices.ContentManager.Create(term);
                }
                _orchardServices.ContentManager.Publish(languageTaxonomy.ContentItem);
            }

            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());
            _orchardServices.ContentManager.Create(widget);
            _orchardServices.ContentManager.Publish(widget.ContentItem);

            ContentDefinitionManager.AlterPartDefinition("ProductPart", cfg => cfg.
                                                                   WithField("Language", field => field
                                                                                                      .OfType("TaxonomyField")
                                                                                                      .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 https://orchard.codeplex.com/discussions/390169 with no answer unfortunately.

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

@Carlbm

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.

 

Developer
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.