Extensions on DetailFormView: Enable Custom Actions on Save
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");
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
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);
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.