This project is read-only.

How to access ContentField value in the view template

Topics: General, Writing themes
Mar 25, 2011 at 4:13 PM

I have a custom content type Testimonial built using admin dashboard (no code). Testimonial has two text fields, Signature and Organization and a Body part. In the theme folder I created a view Views/Items/Content-Testimonial.cshtml  that is responsible for rendering Testimonial in the content zone. Here is the code:

@using Orchard.ContentManagement;
@using Orchard.Utility.Extensions;
@using Orchard.Core.Common.Models;
	BodyPart body = (BodyPart)Model.ContentItem.Get(typeof(BodyPart));	

<h1>@Model.Signature (@Model.Organization)</h1>
<p class="testimonial">"@body.Text"</p>

Line <h1>@Model.Signature (@Model.Organization)</h1> does not work. @body.Text is rendered fine.

How to access values of Signature and Organization text fields fоr the current contentItem?

Mar 26, 2011 at 1:03 AM

It depends what version you're using. We added some new stuff in 1.1 that makes this very easy. Especially if you are using shape tracing.

Mar 27, 2011 at 12:53 PM

I'm using Orchard v1.0.20

Mar 29, 2011 at 11:34 AM

I´d really like to know that as well (using Orchar v1.0.20).

Mar 29, 2011 at 10:39 PM
((TextField)contentItem.As<TestimonialPart>().Fields.First(f => f.Name == "Signature")).Value;

But it's ridiculously easier in 1.1.

Mar 30, 2011 at 2:50 AM

How do you do this in 1.1? I couldn't find anything in Shape Tracing, other than how to override the field template.

Mar 30, 2011 at 7:14 AM

Thank you very much.

Mar 30, 2011 at 7:18 AM

Thank you so much!

Mar 30, 2011 at 8:44 AM

In 1.1, select the shape where you'd want to display the field, then go to the Model tab and deploy the tree to your field. The code to render the field is written for you above the tree.

Mar 30, 2011 at 9:23 AM

That's what I was looking for but it's not there.

- If I go to the Content node, there's no way to navigate to any fields in the Model tab

- If I go to the Fields_Common_Text node, the Model tab only contains ContentPart > ContentItem, which is a reference back to the Page.

Mar 31, 2011 at 12:09 AM

Fields are always attached to a part, never to the item itself. Here:

Mar 31, 2011 at 9:40 AM
((TextField)contentItem.As<TestimonialPart>().Fields.First(f => f.Name == "Signature")).Value;

does not compile because Testimonial is build using content type admin interface. There is no class TestimonialPart in the project folder.

or do I miss a using statement? Is it generated on the fly based on the content type definition?


Mar 31, 2011 at 9:46 AM

Try the following:

var signature = contentItem.Parts.First(p => p.PartDefinition.Name == "Testimonial").Fields.First(f => f.Name == "Signature"as TextField;
Mar 31, 2011 at 10:14 AM

But if I add fields directly to the Content Type using Dashboard (not to a Part) - are you saying it will still be added to a part? How do I know which one? (I think this is the same thing ipavlovi is asking)

Mar 31, 2011 at 10:18 AM

The way I did it can be used with content types (maybe they are being converted into a part internally, I don´t know).

Mar 31, 2011 at 12:51 PM
bertrandleroy wrote:

Fields are always attached to a part, never to the item itself. Here:

Hmm, I just don't have those nodes in Model. Nothing after Parts - I don't see "BodyPart", "CommonPart", or any field names. I checked it against a clean 1.x branch to make sure it wasn't something I'd messed up, still missing. Also "Foo" in your example - is that a content part you've created? I'm adding Fields direct to my Content Type, not to any parts.

Also it's a bit strange how Bar is listed as a sub node of Foo. Expanding the "Parts" node produces this kind of hierarchy also. But after about the 4th nested node, it gets impossible to navigate because it exceeds the width of the column. What happens if I have, say, 10 custom fields on my item?

Mar 31, 2011 at 5:14 PM

Oh I see (partially) - other fields would be listed alongside Bar. Just the way Parts are listed confused me. But I still don't see the fields listing like that on my content item.

Mar 31, 2011 at 10:45 PM

When you create a type from admin, we create a part of the same name. That's where the fields go, as should be clear from the screenshot above: under the content item, there is the Foo part (I created the Foo type from admin), and under Foo, there is that Bar field.

So in 1.0, I could access that with ((TextField)contentItem.Parts.First(p => p.PartDefinition.Name == "Foo").Fields.First(f => f.Name == "Bar").Value

Mar 31, 2011 at 10:59 PM
Edited Mar 31, 2011 at 11:00 PM

Ok, I understand now - the problem is, I still don't have those nodes listed in my ContentItem. It has Id, Version, ContentType, TypeDefinition, Record, VersionRecord, Parts ... none of the actual parts are listed by name as on your screenshot. If I expand the "Parts" node it gets into a weird hierarchy that still doesn't contain the information, and after about the 6th level of expansion overlaps with the other column of text so I can't read it or browse further. Just keep expanding the Parts node and you should see what I mean. Will post a screenshot if you need!

Mar 31, 2011 at 11:03 PM

Yep, it's pretty much impossible to understand what's going on without seeing some type definitions, screenshots, code, etc.

Mar 31, 2011 at 11:33 PM

Ok here we go;

This is just my normal Page type which has an ImageField called BackgroundImage. There's also an ImageField on BodyPart from when I was trying other methods of accessing. This is showing differently than my custom type, but still messing the part and field names.

This is what happens if you keep expanding nodes under that "Parts" node. Eventually it overlaps with the RHS and you can't read anything or expand any further.

Here's a custom content type I created. Should also note that at this point I updated to latest source (and switched to 1.x since I was working with some changes and I wanted to eliminate the possibility I'd messed something up).

I created Foo and added a Bar field. It's still not showing in the model and things have moved around more since I updated. However at this point I noticed that the Data element has the field value in XML form.

Actually I can reach it under Infoset.Element.Value.

Any idea what's going on here?

Mar 31, 2011 at 11:54 PM

Yep. Bug.

Apr 1, 2011 at 12:04 AM


I'm very excited about what Shape Tracing is going to look like by release :)