Setting content fields from custom module

Topics: Customizing Orchard, General, Troubleshooting, Writing modules
Jun 4, 2014 at 6:21 PM
Edited Jun 4, 2014 at 8:49 PM
How do you set a field that you have added to a content type from Visual Studio? I'm trying to create a page from part of an XML doc, and have had no problem setting all the standard parts, e.g.
page.As<TitlePart>().Title = "Some Title";
page.As<BodyPart>().Text = "Body text";
but I haven't been able to figure out how to set the taxonomy fields that I added to the page type definition. The goal is to select the pre-created taxonomy term that matches the title of the page, but I can't figure out how to access the Taxonomy Part/Field/whatever it would be.


EDIT: Nevermind, figured out an easier way to accomplish the same thing.
Jun 4, 2014 at 9:48 PM
Maybe post your solution, at least in part, for future readers with a similar issue?
Jun 5, 2014 at 3:13 PM
I was intending to use the taxonomy to create hierarchical pages, which I initially got around by just setting the Menu Position value for the page as such:
page.As<MenuPart>().MenuPosition = _navigationManager.GetNextPosition(menu);
//appended ".1" to menu position for subpage, etc.
Someone I work with answered my original question though:
//create local var corresponding to desired preexisting taxonomy
 taxName = _taxonomyService.GetTaxonomies().FirstOrDefault(t => t.Name == "Taxonomy Name")

//updates Field Name in page object to IEnum<TermPart> returned by second argument
_taxonomyService.UpdateTerms(page, taxName.Terms.Where(term => term.Name == "Term Name"), "Field Name");
Hope that helps anyone who might have the same issue!