How to get the createdutc date from a part in code

Topics: Customizing Orchard, Troubleshooting
Mar 4, 2013 at 2:48 PM
My part has a related contenttype but this contenttype is not containing directly CommonPart, instead it is attached by driver to the UserPart which contains the CommonPart.
Mar 4, 2013 at 3:26 PM
I'm not following. A content part cannot contain other content parts, correct? Perhaps show us some code that demonstrates your model?
Mar 4, 2013 at 5:29 PM
Edited Mar 4, 2013 at 5:29 PM
This is from your ecommerce sample blog :) CustomerType with CustomerPart and UserPart, I don't want to keep the duplicated createdate and would appreciate to use the one automatically attached to the user part of customer on its creation.
When you query customers you write
var customerQuery = _customerService.GetCustomers().Join<UserPartRecord>().List();

Here I would be able to also get the CommonPartRecord from the userpart to access its createdUtc ?
Mar 4, 2013 at 6:18 PM
Aha, that explains it :)
Yes, if you have a single content item of type Customer which has the CommonPart attached, you can access the CreatedUtc property like so:
var contentItem = _contentManager.Get(42);
var created = contentItem.As<CommonPart>().CreatedUtc;
Or using the dynamic approach:
var contentItem = (dynamic)_contentManager.Get(42);
var created = contentItem.CommonPart.CreatedUtc;
And to create a query that for example sorts by CreatedUtc, do this:
var newestCustomersFirst = _contentManager.Query<CustomerPart, CustomerPartRecord>().Join<CommonPartRecord>().OrderByDescending(x => x.CreatedUtc).List().ToList();
Mar 4, 2013 at 7:09 PM
What is non evident in Orchard implementation of Parts and ContentTypes is the fact that when I create a Cutomer contentPart the ContentItem instanciating the ContentType is automatically created and all its related parts.
This is the case for UserPart which is linked to the Customer Type 'artificially' but have its UserType with CommonPart created automatically, if I correctly undrestand.

As a summarye:

I have ContentType 1 made from parts A and B
And ContentType 2 made with PartB and CommonPart

When I create 1, B is created and consequently 2 is automatically created with commonPart

could you confirm ??
Mar 4, 2013 at 8:33 PM
I'm afraid I'm not following you. Are you saying that when you instantiate a content item of for example Type1, which has PartA and PartB attached, this instantiates a content item of Type2 if Type2 has PartB attached as well? What do you mean when you say "and consequently 2 is automatically created"? Do you mean the ContentType2 is created, or a content item with that type? How are you seeing that?
Mar 4, 2013 at 8:59 PM
Edited Mar 4, 2013 at 9:00 PM
I am not seeing that, simple deduction from what you told me.

When the code create the customer, it also create its user part. (customerdriver)
        protected override DriverResult Editor(CustomerPart part, IUpdateModel updater, dynamic shapeHelper)
            updater.TryUpdateModel(part, Prefix, null, null);

            var user = part.User;
            updater.TryUpdateModel(user, Prefix, new[] {"Email"}, null);

            return Editor(part, shapeHelper);
Here I deduce from what you say that the CommonPart attached to the UserPart IN the User contenttype has been created ?
Did I misunderstand ?

Thanks for time explaining.
Mar 17, 2013 at 7:32 PM
Still not following. CommonPart cannot be attached to the UserPart, because you cannot attach one part to another.
Could you please rephrase the question?
Mar 18, 2013 at 10:10 AM
I have understood this.
I have accepted the fact that it is not possible to migrate easily the existing Users in Customers.