This project is read-only.

ContentPart missing in ContentType

Topics: Writing modules
Jul 24, 2011 at 12:06 PM

Hi, just started getting familiar with Orchard so apologies if I overlook something obvious (i did search before asking).

The issue I'm facing is following. I created a simple ContentPart called "ShopPart":

    public class ShopPartRecord : ContentPartRecord {
        public virtual string Description { get; set; }

    public class ShopPart : ContentPart<ShopPartRecord> {

        public string Name {
            get { return this.As<RoutePart>().Title; }
            set { this.As<RoutePart>().Title = value; }

        public string Description {
            get { return Record.Description; }
            set { Record.Description = value; }

Then I added a handler:

    public class ShopPartHandler : ContentHandler {
        public ShopPartHandler(IRepository<ShopPartRecord> shopPartRepository) {

And I configured the content type:

                table => table

                cfg => cfg
                    .WithPart("AdminMenuPart", p => p.WithSetting("AdminMenuPartTypeSettings.DefaultPosition", "5")));

As far as I understand, that should be it right? (for the type to just "exist"). However, when I ask for a new instance of the Shop type in my controller it's missing "ShopPart" in its Parts collection. As a result of that when I call

ShopPart shop = Services.ContentManager.New<ShopPart>("Shop"); 
I get an exception.
Thanks in advance for advice!
Jul 25, 2011 at 9:10 PM

Are you sure your migration ran?

Jul 25, 2011 at 9:23 PM
Edited Jul 25, 2011 at 9:25 PM

positive, because when I look into the admin screen, I can actually see the "ShopPart" is member of "Shop" content type

I also ran migration which removes the type and adds it again.

Jul 25, 2011 at 9:35 PM

OK, so what exception are you seeing?

Aug 14, 2011 at 3:08 PM

Hi sorry for late response, I really appreciate your help.

The exception is InvalidCastException it is thrown from the New extension method in content extensions: 

public static T New<T>(this IContentManager manager, string contentType) where T : class, IContent {
        var contentItem = manager.New(contentType);
        if (contentItem == null)
            return null;

        var part = contentItem.Get<T>();
        if (part == null)
            throw new InvalidCastException();

        return part;

Thanks in advance for any suggestions.

Aug 14, 2011 at 6:02 PM

I read your code and I can't see any issue in here. If there is an exception at this level it's definitely because the Shop content type doesn't have this part attached. Maybe you have two Shop content types, some corruption in the db. Did you try with a new Website ?

Aug 16, 2011 at 8:15 PM

Tried with brand new site and still no success. It's a mystery, the ContentTypeDefinition which is fetched in New method contains the "ShopPart" as expected. Then the handlers are invoked (handler above for the ShopPart as well) and composite result is returned as ContentItem, without the ShopPart though.

I bet I overlooked something veery simple. If interested the module can be found here.


Aug 16, 2011 at 9:30 PM
Edited Aug 16, 2011 at 9:31 PM

Seems that you're missing the driver. For the moment, even if empty, a driver is still required for every part so that it can be properly attached to the content type.

Aug 17, 2011 at 8:06 PM

That was the obivous :)

Now it's working, thank you all, your help is much appreciated!