Deleting Event for Dynamic Module item

Posted by Community Admin on 04-Aug-2018 08:33

Deleting Event for Dynamic Module item

All Replies

Posted by Community Admin on 13-Nov-2013 00:00

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

Posted by Community Admin on 13-Nov-2013 00:00

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");
    //

Regards,
Boyko Karadzhov
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 Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 13-Nov-2013 00:00

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

Posted by Community Admin on 13-Nov-2013 00:00

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

Posted by Community Admin on 15-Nov-2013 00:00

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?

Posted by Community Admin on 18-Nov-2013 00:00

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.

Regards,
Boyko Karadzhov
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 Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 18-Nov-2013 00:00

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.

Posted by Community Admin on 21-Nov-2013 00:00

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

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 Public Issue Tracking system and vote to affect the priority of the items

This thread is closed