welding a custom part to content item

Topics: Writing modules
Nov 9, 2012 at 8:45 PM
Edited Nov 9, 2012 at 9:26 PM

experimenting in content item handler to weld a custom part on demand to a content item i used this code : 

 

  public PartyIDHandler(IRepository<PartyIDPartRecord> partyRepository, IAppController appController)
        {
            _appController = appController;
            Filters.Add(StorageFilter.For(partyRepository));
            OnLoaded<PartyIDPart>(LoadDataFromDb);
            OnGetDisplayShape<PartyIDPart>(AttachCustomPart);
           
        }

            void AttachCustomPart(BuildDisplayContext context, PartyIDPart part) {
                //this gives us access to dispaly type and shape property as well as content item
                var cntitem = context.ContentItem;
               
                //weld custom part
               var usercommandspart = new UserCommandPart {Prop1 = "prop1111", Prop2 = "prop2222" };
               cntitem.Weld(usercommandspart);
}

well this code fails. 

after a while in order to get it to work i had to pass in a value for custom part property =TypePartDefinition as shown below. 

 

  public PartyIDHandler(IRepository<PartyIDPartRecord> partyRepository, IAppController appController)
        {
            _appController = appController;
            Filters.Add(StorageFilter.For(partyRepository));
            OnLoaded<PartyIDPart>(LoadDataFromDb);
            OnGetDisplayShape<PartyIDPart>(AttachCustomPart);
           
        }

            void AttachCustomPart(BuildDisplayContext context, PartyIDPart part) {
                //this gives us access to dispaly type and shape property as well as content item
                var cntitem = context.ContentItem;
               
                //weld custom part
                // If the content item's type definition does not define the part; use an empty type definition.
             var   typePartDefinition = new ContentTypePartDefinition(
                    new ContentPartDefinition("UserCommandPart"),
                    new SettingsDictionary());
             var usercommandspart = new UserCommandPart { TypePartDefinition = typePartDefinition, Prop1 = "prop1111", Prop2 = "prop2222" };
             cntitem.Weld(usercommandspart);
}

 

 

my question is  if its necessary to define the  TypePartDefinition as above?

The first approach seems more straightforward and easier. 

if its necessary to supply this property should'nt it be obvious to developer to supply this value (perhaps through the constructor)

giannik.

Developer
Nov 9, 2012 at 10:32 PM

If you are looking to weld on a content part to content items of a specific type, all you need to do is add an ActivatingFilter<TPart>:

Filters.Add ( new ActivatingFilter<MyPart>("MyContentType") );

Nov 10, 2012 at 7:54 AM

yes but its not very flexible. 

i cannot  pass in property values to the part. 

The handler is better in my case , just need to confirm if my approach is correct and wont run into problems later. 

 

Developer
Nov 10, 2012 at 8:24 AM
Edited Dec 25, 2012 at 6:10 PM

Ok. Yes it looks like the content part needs the TypePartDefinition. However, you don't have to do that yourself if you handle the Activating event, which gets a context with a ContentItemBuilder object, which has a Weld method that does all the required things like setting a TypePartDefinition. Checkout the implementation of the ActivatingFilter to see how this works.

Nov 10, 2012 at 9:59 AM

thanks sipke  

for confirming my assumptions and guiding to solution.