How Add Parts to Content Item intsead of Content Type

Topics: Core, Writing modules
Dec 4, 2014 at 10:06 AM
I want to dynamically add parts to content item instead of adding them to content type. Is it possible? If yes then How?

-Thanks
Dec 4, 2014 at 12:02 PM
Content Item and Content Type is like instance and a class ... And ContentPart is as another class which is associated with Content Type

Give more details about this case.
Dec 4, 2014 at 2:48 PM
Edited Dec 4, 2014 at 2:50 PM
Thank you Daniel,

My requirement is I can't have Content Parts attached to my Content Type. I want to have Content Items of Same Content Type have different Content Parts attached to it, because which parts gets attached to the content item will depends on the conditions.


For example:
Content Type --> A
Content Items --> A1 with Part: Media, A2 with Parts: Navigation, CustomPart
Thanks
Dec 4, 2014 at 4:15 PM
Could you elaborate more on why you want to do that?

In my opinion if your Content Item has different parts than another, it should be another Content Type?
Dec 4, 2014 at 8:39 PM
I think it is impossible .. you must to model your solution other way .. Give more real example to see the point ... maybe it would be archived other way.
Developer
Dec 5, 2014 at 3:34 AM
Edited Dec 5, 2014 at 3:35 AM
I haven't tried it myself, but it looks like you should be able to achieve that like this:
ContentItem contentItem = ...;
MyPart part = new MyPart();

contentItem.Weld(part);
The Weld method on the ContentItem class will add the specified part to its Parts collection:
public void Weld(ContentPart part) {
   part.ContentItem = this;
   _parts.Add(part);
}
Marked as answer by cloudsurfer on 12/5/2014 at 2:37 AM
Dec 5, 2014 at 10:37 AM
Edited Dec 5, 2014 at 10:37 AM
Yes. It works. Thanks a bunch spike.