How to access fields of a content item again?

Topics: Troubleshooting, Writing modules
Developer
Mar 1, 2012 at 9:57 PM

In Orchard 1.3, I was able to cast a content part to dynamic, and then access any attached fields to any of the other parts attached to the content item. The beauty of this is that it works even for content parts created on the fly, which have no class representation.

Example:

ProductPart productPart = LoadProduct(1);

// The productPart just loaded is actually part of a Book content item. The Book content type has a TextField called "Author"

dynamic product = productPart;

// The following line works in 1.3; not in 1.4
var authorName = product.Book.Author.Value;


What I need to know is: has this Clay behavior changed, or should it still work? If it's the latter, I'm probably overlooking something, but then I at least know that it should work the way I did above.

Developer
Mar 1, 2012 at 10:13 PM
Edited Mar 2, 2012 at 8:12 AM

Never mind, it still works. The problem is that there was no Book part whatsoever. In 1.3, I created the Book content type via the admin UI (which will secretly create a content part with the same name as soon as you add a field to it). In 1.4, I instead executed a migration that created the Book content type, but that will not create a Book content part for me (as the admin UI would have done).

Developer
Mar 2, 2012 at 7:21 AM
Edited Jun 6, 2013 at 8:12 PM
How nice, I didn't know it could work as a dynamic this way. BTW if you want statically typed fields, in my Helpful Libraries module there is a class for that (add extensions to content classes to cast to fields just as to parts).
Developer
Mar 2, 2012 at 8:11 AM

Cool, I didn't know about your library. Thanks.