1
votes

I have a content item called Event, which has a taxonomy field called Section attached via the content definition area.

What is the easiest way to retrieve the Section field from the content within an alternate? My alternate is not overriding an Event, so Model.ContentItem is not possible. Within my alternate my Event object instance is of type ContentItem which I am retrieving through the ContentManager.

This is what i'm doing at the moment:

ContentItem content = WorkContext.Resolve<IContentManager>().Get(id);
var = content.Parts.ElementAt(13).Fields.ElementAt(0);

I realise that in the above code the index's could change, the only other way I can think of doing this is by inserting Lambda expressions in the place of the integers.

content.Parts.ElementAt(13) returns object of type ContentPart content.Parts.ElementAt(13).Fields.ElementAt(0) returns object of type TaxonomyField. Whereas I believe I need the TermPart?

If it cannot be achieved in a simple way, why is it so difficult to perform such a simple task?

Thank you in advance.

1

1 Answers

2
votes

First you do not need the ContentManager on the template.

On the Model you have the ContentItem. You can retrive the field like this:

var contentItem = Model.ContentItem;
var terms = contentItem.Event.TaxonomyFieldName.Terms;

On terms you have the terms of the ContentItem.