ContentPicker - Rendering Item

Topics: Writing modules, Writing themes
Sep 12, 2012 at 12:02 AM
Edited Sep 12, 2012 at 12:05 AM

Using the ContentPicker field - how do I render the actual item that the ContentPicker points to?

I actually have the ContentItem (verify the id values etc) - but how do I @Display it?

When I write @Display( pickedItem ) - all I get is the field type "Orchard.ContentManagement.ContentItem"; how do I render the actual item. (the resulting HTML from the ContentItem)


Sep 12, 2012 at 4:46 PM

Display is for shapes, not for content items. You would need to call BuildDisplay on the content manager in order to generate a shape that you could then display.

Sep 12, 2012 at 11:42 PM

How is that accomplished from within a view/template? (e.g. from the Layout.html).

The goal is that instead of creating hundreds of layers for the different content items that below to one page or another; I'd rather create hundreds of content items and then use the content picker from each page type to select what content items shuld be rendered in the Content zone while the page is rendering.

Currently - it only renders properties of the ContentItem but not the resulting html (as specified by the cshtml that is applied to that content item).

Sounds like BuildDisplay would work because it would go out and do what it would normally do if the item was being rendered; but would like to-do this from a .cshtml file.


Sep 12, 2012 at 11:55 PM

Well, frankly I don't recommend doing it from the view, but if you have no other choice, you just get an instance of IContentManager from the WorkContext and then you Get or GetMany on it to get the items, then you do BuildDisplay.

Sep 13, 2012 at 1:29 AM
Edited Sep 13, 2012 at 1:36 AM

Fortunately I don't have to use the "get" because the Content Item Picker already has it; so I got it to work properly.

So my question is Why isn't this the best practice - how else would I be able to set and get these items and render them?  I'm using a content type that I built from the Admin UI - and it's not very clear on how to override anything in code (even if I have my own module) shapes that are built within the UI.

What if all I had was a theme and custom types built from the Admin UI - how would it be possible to use the content picker as a field to pull content?  I only chose to-do this via the cshtml because I can call the below function within my cshtml file and render the contents within the zone.

If there's a more elegant or better way to accomplish this - please help - or point me to the documentation that would help me.  I'd much rather use non-code in the views; it's just not clear how to override shape rendering for ContentTypes that are generated within the Admin UI. (since the classes aren't marked partial). So that noone reading this thread gets confused "CustomPropertiesPart.ContentItems" is the actual name of the ContentPickerField when I added it to my custom part. It's a content picker that will allow an author to select items that will be rendered in the "Content" zone.  I was doing this with Layers first - but I would have hundreds of layers for all the design types; now I don't - I accomplish the same exact thing; but per page - and still have the ability to use zones when necessary.


 @AppendShape( Model.ContentItem.CustomPropertiesPart.ContentItems, "content" );

    void AppendShape(ContentPickerField field, string zone)
        var contentManager = WorkContext.Resolve<IContentManager>();
        var items = field.ContentItems;
        foreach (var item in items)
            var shape = contentManager.BuildDisplay(item, "Detail");
            if ( string.Equals(zone,"content", StringComparison.OrdinalIgnoreCase ) )

Any insight would be greatly appreciated.

Thanks - bill  

Sep 13, 2012 at 7:14 PM

This is not best practice because it forces you to do from the view operations that should be performed in the driver. Maybe what the content picker field should be doing is add factories for those shapes onto the field's shape. That way, there wouldn't be a cost (because the actual shapes only get built on-demand) and your views would remain clean. Content picker field is young. Maybe that would be a useful suggestion to make in the issue tracker?

Apr 12, 2013 at 4:38 AM
I found a new thing named ContentPicker Part from sfmskywalker's ContentPickerWidget. It wrapped the ContentPicker Field with a display option (DisplayType: Detail, Summary..)

It helps me to render the real content items.
May 31, 2013 at 6:08 PM
Tanza9 - Can you tell me how to get the ContentPickerWidget to work? Thanks.