Access Taxanomies field in cshtml

Jul 5, 2011 at 9:17 AM

I have created a custom type with taxonomies field (named "Colours") but i dont know how to access to render the taxonomies field in cshtml. When call @Display(Model.ContentItem.{ContentType}.Colours only display the value "Contrib.Taxonomies.Fields.TaxonomyField"

Using Shape Tracing to view the model, the Colours only has field's name

Question: How to render all terms of taxonomies field associated with the content item.

Thanks for helping

Jul 6, 2011 at 8:09 AM

Please help!!!

Jul 6, 2011 at 4:08 PM

I don't understand the question. If you have associated some terms to a content item, then they should appear automatically when you display the content item. Don't they ?

If you want to customize this list, then you can override the Fields_Contrib_TaxonomyField.cshtml file in your theme, as shown by the Shape Tracing. Another discussion on this forum is explaining how to do it, even showing a custom field of a taxonomy field ... clever.

Jul 6, 2011 at 4:25 PM

I want to customize template to display detail of content items with taxanomies field.

Please see my template:


dynamic p = Model;             

string title = p.Title;

var colours= p.ContentItem.Product.Colours; //it will display string "Contrib.Taxonomies.Fields.TaxonomyField", I want to display all terms of Colours (Green, Red....)


<div class="spec">
                        <span style="font-weight: bold; text-transform: uppercase;">@title</span>
                        <span>@T("Colours: ")@colours</span>


I think, if override Fields_Contrib_TaxonomyField.cshtml, in template must be @Display(Model.Content)

Hope you can understand,

Thanks for your help

Jul 23, 2011 at 8:46 PM

I have the same question, I want to place the category name in a custom area on the page, not in the "Meta" or "Content" sections, but rather place it somewhere specifically in the Content parent template. How do I access the list of terms programmatically?

Aug 14, 2011 at 3:21 PM
Edited Aug 14, 2011 at 3:21 PM

I have the same sort of question.

In my situation, I have a content type named "Brochure" with two Taxonomy Fields attached: "Languages" and "Category".
The 'Languages' taxonomy contain multiple verbs, one of which is named 'English'.
When I navigate to /brochures/english, I see a list of my Brochure items which are associated with the verb 'English'.

So far so good.

The thing is that my Brochure items have also a Category taxonomy.
What I want to do is display all English brochures, but grouped by their Category taxonomy verbs.

So let's say I have some English brochures, 3 of which have the "Industrial" category and 4 of which have the "Nature" category, I want to build up a dictionary with the category as a key and the Brochures as a value.
In order to do so I need to be able to ask my Brochure content item which verbs are attached to it.

I'm looking to be able to do something like this:



@using Contrib.Taxonomies.Fields
@using Orchard.ContentManagement
    IEnumerable<object> contentItems = Model.ContentItems;

    foreach (ContentItem contentItem in contentItems)
        var attachedCategories = contentItem.Field<TaxonomyField>("Category").Items.ToArray();
        // Store the content item in a dictionary used for rendering a list of Brochures grouped by the Category taxonomy
@if (contentItems == null || contentItems.Count() < 1) {
<p>@T("There is no content associated to this term.")</p>


Obviously the line  in bold-face in the foreach loop is not going to work, but it demonstrates what I need to be able to do: retrieve the associated verbs of a certain taxonomy.

Apr 4, 2012 at 12:01 AM

I have exactly the same issue as hairbeos and SelArom. I can't access the terms from the model. I can see the field, but can only seem to access the name of the field directly.  I am using version 0.9 as I am still on Orchard 1.3.

Any idea?

Apr 4, 2012 at 7:29 AM
Edited Apr 4, 2012 at 7:31 AM

The way I do it is just use the ITaxonomyService:

To get a list of terms for a specific taxonomy:

private IList<TermPart> GetTerms(string taxonomyName)
            var taxonomy = _taxonomyService.GetTaxonomyByName(taxonomyName);
            var terms = _taxonomyService.GetTerms(taxonomy.Id).ToArray();
            return terms;

To get the assigned terms from a specific content item:

IEnumerable<TermPart> assignedTerms = _taxonomyService.GetTermsForContentItem(contentItemId, "NameOfTaxonomy");



Apr 10, 2012 at 2:04 AM

Thanks! That seems like a solution. One question - how do you initialize or call  ITaxonomyService from a view in a Theme? 

Apr 10, 2012 at 2:32 AM

Never mind. I got it...thanks heaps again!

Apr 19, 2012 at 5:25 PM

Hey Brainwrecker - I have the same question about initializing the taxonomyService from a view. How did you do it?


Apr 20, 2012 at 5:39 AM
Edited Apr 20, 2012 at 5:39 AM

I initialized it using the following code :

var _taxonomyService = WorkContext.Resolve<ITaxonomyService>();

Don't forget to add Taxonomy Module reference to  your theme. 

Apr 20, 2012 at 5:42 PM

that works perfectly, thanks!

Mar 20, 2013 at 4:40 PM
I still cannot initialize the _taxonomyService with this line of code in my view file (.cshtml)
              var _taxonomyService = WorkContext.Resolve<ITaxonomyService>();
I get this error : An object reference is required for the non-static field, method, or property 'Orchard.WorkContext.Resolve<T>
What is going wrong?When brainwrecker said that i have to add the taxonomy module reference in my theme what exactly
did he mean?
Thanx in advance!
Mar 20, 2013 at 4:48 PM
WorkContext is a class here.

From a view, WorkContext is a property from the base View, so it works. From a class you need to inject a IWorkContextAccesor, then do _workContextAccessor.GetWorkContext().Resolve<T>(), or even better, inject T directly in your constructor.
Mar 20, 2013 at 9:39 PM
I m in a view file but it doesnt work.How can I initialize taxonomyService in a view file?
My goal is to make a query similar to this

var _taxonomyService = WorkContext.Resolve<ITaxonomyService>();

var termContentItems = _taxonomyService.GetContentItems(termname).Where(c => c.ContentItem.ContentType == "YourContentTypeName").ToList();
Apr 1, 2013 at 9:59 AM
var _taxonomyService = WorkContext.Resolve<ITaxonomyService>(); works from within a view in Orchard. If not, can you show us the exact error you see?
Apr 2, 2013 at 6:29 AM
Thank you for your answer!
This code did finaly work for me
var _taxonomyService = WorkContext.Resolve<ITaxonomyService>();

    var termContentItems = _taxonomyService.GetContentItems(@Model.Terms[0]);
Jun 5, 2013 at 2:44 PM

I am trying to use :

var _taxonomyService = WorkContext.Resolve<ITaxonomyService>();

from Providers\layoutShapea.cs in my project, but i have the following erro :" An object reference is required for the non-static field, method, or property 'Orchard.WorkContext.Resolve<T>() "

any sugestion how to hover come this erro ?

Jun 9, 2013 at 11:24 PM
Could you show the code where you are actually initializing WorkContext?
Jun 11, 2013 at 8:36 AM
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Mvc.Html;
using Orchard.Utility.Extensions;
using Contrib.Taxonomies.Fields;
using Contrib.Taxonomies.Services;
using Contrib.Taxonomies.Models;

namespace Orchard.Projections.Providers.Layouts
public class LayoutShapes : IDependency
    public LayoutShapes()
        T = NullLocalizer.Instance;

    public Localizer T { get; set; }

    public void Protofolio(dynamic Display, TextWriter Output, HtmlHelper Html, string Id,
                        IEnumerable<dynamic> Items,
                        IEnumerable<string> OuterClasses, IDictionary<string, string> OuterAttributes,
                        IEnumerable<string> InnerClasses, IDictionary<string, string> InnerAttributes,
                        IEnumerable<string> FirstItemClasses, IDictionary<string, string> FirstItemAttributes, 
                        IEnumerable<string> ItemClasses, IDictionary<string, string> ItemAttributes)
        if (Items == null) return;

        IEnumerable<string> rowClass = new List<string>() { "row" };
        IEnumerable<string> span12Class = new List<string>() { "span12" };
         IEnumerable<string> buttonClass = new List<string>() { "button" };
        IEnumerable<string> EmptyClass = new List<string>() { "" };
        var items = Items.ToList();
        var itemsCount = items.Count;

        if (itemsCount < 1) return;

        var rowDivTag = GetTagBuilderWithoutAtrribute("div", string.Empty, rowClass);
        var span12DivTag = GetTagBuilderWithoutAtrribute("div", string.Empty, span12Class);
        var buttonDivTag = GetTagBuilderWithoutAtrribute("div", string.Empty, buttonClass);
        var ulTag = GetTagBuilderWithoutAtrribute("ul", "filters", EmptyClass);
        var liTag = GetTagBuilderWithoutAtrribute("li", string.Empty, EmptyClass);

        var portfolioDivTag = GetTagBuilderWithoutAtrribute("div", "portfolio",EmptyClass);
        var outerDivTag = GetTagBuilder("div", Id, OuterClasses, OuterAttributes);
        var innerDivTag = GetTagBuilder("div", string.Empty, InnerClasses, InnerAttributes);
        var firstItemTag = GetTagBuilder("div", string.Empty, FirstItemClasses, FirstItemAttributes);
        var itemTag = GetTagBuilder("div", string.Empty, ItemClasses, ItemAttributes);


        int i = 0;

        foreach (var item in items)
            //if (i == 0)
            //    Output.Write(firstItemTag.ToString(TagRenderMode.StartTag));



        Output.Write("<script src=\"/Themes/O2energy/Scripts/jquery.isotope.js\" type=\"text/javascript\"></script>");
        Output.Write("<script>var $container = $('#portfolio');$container.isotope({});</script>");


    private object GetTagBuilder(string p1, string p2, string p3, string p4)
        throw new NotImplementedException();

    static TagBuilder GetTagBuilder(string tagName, string id, IEnumerable<string> classes, IDictionary<string, string> attributes)
        var tagBuilder = new TagBuilder(tagName);
        tagBuilder.MergeAttributes(attributes, false);
        foreach (var cssClass in classes ?? Enumerable.Empty<string>())
        if (!string.IsNullOrWhiteSpace(id))
        return tagBuilder;
    static TagBuilder GetTagBuilderWithoutAtrribute(string tagName, string id, IEnumerable<string> classes)
        var tagBuilder = new TagBuilder(tagName);

        foreach (var cssClass in classes ?? Enumerable.Empty<string>())
        if (!string.IsNullOrWhiteSpace(id))
        return tagBuilder;
       private IList<TermPart> GetTerms(string taxonomyName)
        var _taxonomyService = WorkContext.Resolve<ITaxonomyService>();

        var taxonomy = _taxonomyService.GetTaxonomyByName(taxonomyName);
        var terms = _taxonomyService.GetTerms(taxonomy.Id).ToArray();
        return terms;