Deleting Event for Dynamic Module item
Hi,
I need to achieve the following with my Dynamic Module:
When a Dynamic Module item is deleted, I need to delete the corresponding DocumentLibrary based on the UrlName of the Dynamic Content item.
I checked the IDynamicContentDeletedEvent interface, but it doesn't have any properties about the deleted item. Probably need an additional event here, like IDynamicContentDeletingEvent.
Is there any way I could achieve such functionality?
Thanks,
Daniel
Hello,
It is true that when Deleted event is raised the deleted item cannot be retrieved. Since we currently do not have "Deleting" event you can attach to the provider's events to track what's going on. Here is a sample how to get the UrlNames of all deleted dynamic content items in Global.asax:
protected
void
Application_Start(
object
sender, EventArgs e)
Bootstrapper.Initialized += Bootstrapper_Initialized;
protected
void
Bootstrapper_Initialized(
object
sender, Telerik.Sitefinity.Data.ExecutedEventArgs args)
if
(args.CommandName ==
"Bootstrapped"
)
var provide = DynamicModuleManager.GetManager().Provider;
provide.Executing += Provider_Executing;
provide.Executed += provide_Executed;
private
void
Provider_Executing(
object
sender, Telerik.Sitefinity.Data.ExecutingEventArgs e)
if
(!(e.CommandName ==
"CommitTransaction"
|| e.CommandName ==
"FlushTransaction"
))
return
;
var provider = sender
as
DynamicModuleDataProvider;
if
(provider ==
null
)
return
;
HashSet<
string
> deletedItemUrls = provider.GetExecutionStateData(
"DeletedItemUrls"
)
as
HashSet<
string
>;
if
(deletedItemUrls ==
null
)
deletedItemUrls =
new
HashSet<
string
>();
var dirtyItems = provider.GetDirtyItems();
for
(
int
i = 0; i < dirtyItems.Count; i++)
var item = dirtyItems[i]
as
DynamicContent;
if
(item ==
null
)
continue
;
var itemStatus = provider.GetDirtyItemStatus(item);
if
(itemStatus == Telerik.Sitefinity.Security.SecurityConstants.TransactionActionType.Deleted)
deletedItemUrls.Add(item.UrlName);
if
(deletedItemUrls.Count > 0)
provider.SetExecutionStateData(
"DeletedItemUrls"
, deletedItemUrls);
private
void
provide_Executed(
object
sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
if
(e.CommandName !=
"CommitTransaction"
)
return
;
var provider = sender
as
DynamicModuleDataProvider;
if
(provider ==
null
)
return
;
var deletedItemUrls = provider.GetExecutionStateData(
"DeletedItemUrls"
);
//
Hi Boyko,
Thanks for the reply and solution. This worked as expected!
Any chance a Deleting event will be part of the EventHub for Dynamic Modules?
Thanks,
Daniel
Hi Boyko,
I still have an issue regarding the code you provided.
If I open an existing Dynamic Content Item and then choose to NOT publish, but instead choose 'Back to projects list', the code determines a Deleted state. So apparently when you publish or just cancel it seems like different versions are created and older ones deleted.
Not sure how this works, but is there a way to know that the item is really deleted?
Thanks,
Daniel
Daniel, that would be the Temp item being deleted I imagine.
Having never used Provider event hooks, I'm curious - can you successfully inspect item.Status somehwere in there and only trigger actions if it's the Master item being deleted?
Hi,
Yes, Stephen, Temps also trigger the event and they are deleted when editing of an item is cancelled.
You can inspect the status of the item. On Executing all pending items are in the dirty items of the provider. The provider is the sender of the event and the dirty items are available via GetDirtyItems() method. You can inspect each item separately to see its Status as well as any other public property.
That's gold! This would have probably come in handy during a recent 6.1 experience where the IComment events were no longer being triggered... Could have simply hooked to the provider in some way.
Hello,
Even without the feature it is possible to achieve the desired effect. It is true though that using the EventHub is much prettier than the Executing/Executed pattern.
Regards,
Boyko Karadzhov
Telerik