Extensions on DetailFormView: Enable Custom Actions on Save

Posted by Community Admin on 05-Aug-2018 21:12

Extensions on DetailFormView: Enable Custom Actions on Save

All Replies

Posted by Community Admin on 27-Apr-2015 00:00

Using:

 Telerik.Sitefinity.Samples.Products/Products/Web/UI/ProductsDefinitions.cs 

as my primary reference, I have been looking into a way to​ extend the DetailFormView. For instance, I would like to attach additional functionality to the events ​triggered when publishing or Saving a Product as a draft. 

Using this reference:

How to add predefined values to the backend create view of Dynamic content items

I was able to gleam a small amount of information about how one might approach this by attaching additional Javascript to the page, but besides that I am at a bit of a loss as to where or how to get a list of all these client-side events...

All of this is done client-side, by a control called DetailFormView. This is one of our most complex controls as it accommodates the create/edit functionality for all content types in Sitefinity. As such we have exposed certain extensibility points which we can benefit from, and  plug some custom logic. This is done via an Extension script. In the extension script we can hook up to a particular event thrown by the DetailFormView and work with the items we get at this point.​

 There's another thread:

How to extend a backend view (Insert) of a custom module

 Which looked promising, but seems a bit dated after looking at the most up-to-date implementation in the github codebase:

From  Telerik.Sitefinity.Samples.Products/Products/Web/UI/ProductsDefinitions.cs:

//Product Image
var productImageField = new ImageFieldElement(mainSection.Fields)
    ID = "avatarField",
    DataFieldName = "ProductImage",
    DisplayMode = displayMode,
    UploadMode = ImageFieldUploadMode.Dialog,
    Title = "ProductImageFieldTitle",
    WrapperTag = HtmlTextWriterTag.Li,
    CssClass = "sfUserAvatar",
    ResourceClassId = typeof(ProductsResources).Name,
    DataFieldType = typeof(ContentLink),
    DefaultSrc = "~/SFRes/images/ProductCatalogSample/Images.NoProductImage.png", // put your default image location example:
    SizeInPx = 100
;
mainSection.Fields.Add(productImageField);

 Plus, it really is addressing adding additional fields and functionality within the page, not hooking into existing functionality or events like the publish/save a draft.

 

My goal is to do something like this (Telerik.Sitefinity.Samples.Products/SitefinityWebApp/Global.asax.cs):

private void CreateSampleWorker(object[] args)
                       
    ...
    var result = SampleUtilities.CreatePage(new Guid(ProductsPageId), ProductsPageName, true);
 
    if (result)
    
        SampleUtilities.SetTemplateToPage(new Guid(ProductsPageId), new Guid(SamplesTemplateId));
 
        ProductsView productsView = new ProductsView();
        SampleUtilities.AddControlToPage(new Guid(ProductsPageId), productsView, "Content", "Products Widget");
               

upon publishing or saving a product as a draft.

Posted by Community Admin on 30-Apr-2015 00:00

Hi Merritt,

In order to execute your custom logic when creating or editing a product, I can suggest that you attach to the IDataEvent which is a global event for all content items. The event will be thrown on Create, Update and Delete actions for all content. 

You may take a look at the following articles for more details about the event:

1. IDataEvent
2. EventHub - capturing global (aplication-wide, non type-dependent) events using IDataEvent

 
As described in the above resources, in the Content_Action() method you can get information about the Action, ItemType and the actual content item.

In order to execute the logic only when products are created or updated you need to check if the contentType.BaseType.FullName is Telerik.Sitefinity.Ecommerce.Catalog.Model.Product and then to check the action (if it is "New", "Updated", "Deleted"). Here is a sample code for your convenience:
 

public class Global : System.Web.HttpApplication
    
 
        protected void Application_Start(object sender, EventArgs e)
        
            Bootstrapper.Initialized += new EventHandler<Telerik.Sitefinity.Data.ExecutedEventArgs>(this.Bootstrapper_Initialized);
        
 
        protected void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
        
            if (e.CommandName == "Bootstrapped")
            
                EventHub.Subscribe<IDataEvent>(Content_Action);  
                      
        
 
        private void Content_Action(IDataEvent @event)
        
            var action = @event.Action;
            var contentType = @event.ItemType;
 
            if (contentType.BaseType.FullName == "Telerik.Sitefinity.Ecommerce.Catalog.Model.Product")
            
                if (contentType.Name == "sf_ec_prdct_generalproduct")
                
                    if (action == "Updated" || action == "New")
                    
                        // execute your custom logic here
                        var itemId = @event.ItemId;
                        var providerName = @event.ProviderName;
                        var manager = ManagerBase.GetMappedManager(contentType, providerName);
                        var item = manager.GetItemOrDefault(contentType, itemId);
                    
                              
            
        
    

In addition to this if you would like to execute the code depending on the product type, you can also check if the contentType.Name is equal to the specified product type. In  the above sample the code will be executed only for the products of type General product.

Regards,
Sabrie Nedzhip
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 07-May-2015 00:00

Very informative - thanks.

 Quick follow-up, say you want tweak the layout for the DetailForm or MasterFormView associated with a Dynamic Module Content Type: is there a way to hook into the configuration to override the template used? If that is too vague don't worry about it, just looking at all potential extensibility options with Sitefinity.

This thread is closed