This project is read-only.

Accessing content body from a custom content part

Topics: Writing modules
Apr 6, 2011 at 2:40 AM

Hello there,

I have a basic question, but I couldn't find any documentation or code in other modules that could help me answer it:

Imagine I have a content part added to a content type. How can I access the content body (or content from other parts) from my part? Is it possible to do that for any generic content type, without knowing exactly which type it is beforehand?

E.g. A content part that displays the character count of its content type instance's body if that type has a "Body" part.


Thanks for your help!


Apr 6, 2011 at 3:59 AM

It's dead easy. You need to reference Orchard.ContentManagement because it's an extension method in that namespace but you just do:


So you have to know the Type of the part you're after, but you can get there from any Part, Content Item, or IContent.

Apr 6, 2011 at 4:07 AM

Thanks a lot!

I imagined it would be simple, but this extension method is not very discoverable... (it's not in the docs, it isn't very visible in any sample modules, and, well, it's called "As<T>()"). 

Apr 6, 2011 at 7:08 AM

In 1.1 you can do part.ContentItem.BodyPart.Body ... There is no intellisense though as it is dynamic, but the shape tracing can show it.

Apr 9, 2011 at 5:42 PM

Just for archiving, my final solution was:

if (this.ContentItem.Has<BodyPart>())
    var text = this.ContentItem.Get<BodyPart>().Text;
    // ...

Thanks for your help! Waiting for Orchard 1.1 next week!