This project is read-only.

Get Taxonomy Field Value in Service Class

Topics: Customizing Orchard, Writing modules
Jul 17, 2013 at 3:25 PM
I am having a little issue when trying to retrieve the actual selected value of a taxonomy field within a custom content in my service class. This is what I do now:
        IEnumerable<ContentItem> jobs = _contentManager.Query("Job_DE").List();
        List<JobItem> jobItems = new List<JobItem>();
        var contentItems = jobs as ContentItem[] ?? jobs.ToArray();
            if (jobs != null && contentItems.Any())
                foreach (ContentItem job in contentItems)
                    JobItem _jobItem = new JobItem {ID = job.Id};

                    foreach (ContentPart jobPart in job.Parts)
                            foreach (var field in jobPart.Fields)
                                if (field.Name == "JobRegion")
                                    TaxonomyField taxoField = field as TaxonomyField;
                                    if (taxoField != null) //Until here it works fine...
                                        _jobItem.Region = taxoField.Terms.Value;
I tried to find out how to handle this through debugging but could not see the values.
Please help.
Greets AL
Jul 17, 2013 at 8:53 PM
Your selected term(s) should be in taxoField.Terms (I'm not sure how you have a Value there?) BTW in Helpful Libraries there is an extensions for that field-finding logic.
Jul 17, 2013 at 8:56 PM
"field finding logic" :/

dynamic content = yourContentItem;
var field = content.PARTNAME.FIELDNAME;

Do you really need some helper code for that ;)
Jul 17, 2013 at 9:04 PM
Yes because it's statically typed. And as much as I love dynamics sometimes I prefer to have it static here :-).
Jul 17, 2013 at 10:34 PM
Edited Jul 17, 2013 at 10:34 PM
updating then:

Jul 17, 2013 at 11:57 PM
I prefer all this (or rather: the logic behind the dynamic call) hidden behind a method similar to the As<TContent>() method instead of doing it the dynamic way.
Jul 18, 2013 at 1:29 AM
Ah yes - this helps massive bros - if ya would know how to get the taxo field selected values ya'd already had told me ;). I am on Service Level so the easy cheasy calls like in the Views ain't helpin' fo' real ...
Jul 18, 2013 at 1:40 PM
This is how it works ;):
                    // get the location from taxonomy jobRegion
                    IEnumerable<TermPart> assignedTerms = _taxonomyService.GetTermsForContentItem(job.Id, "JobRegion");
                    var termParts = assignedTerms as TermPart[] ?? assignedTerms.ToArray();
                    if (termParts.Any())
                        foreach (TermPart term in termParts)
                            _jobItem.Region = term.Name;
Thanks anyway.