+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Deleting Event for Dynamic Module item

Deleting Event for Dynamic Module item

8 posts, 0 answered
  1. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    13 Nov 2013
    Link to this post
    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
  2. Boyko Karadzhov
    Boyko Karadzhov avatar
    56 posts
    Registered:
    05 Dec 2016
    13 Nov 2013
    Link to this post
    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
  3. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    13 Nov 2013 in reply to Boyko Karadzhov
    Link to this post
    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
  4. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    13 Nov 2013 in reply to Daniel Plomp
    Link to this post
    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
  5. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    15 Nov 2013
    Link to this post
    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?
  6. Boyko Karadzhov
    Boyko Karadzhov avatar
    56 posts
    Registered:
    05 Dec 2016
    18 Nov 2013
    Link to this post
    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
  7. Stephen2
    Stephen2 avatar
    94 posts
    Registered:
    05 Feb 2012
    18 Nov 2013
    Link to this post
    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.
  8. Boyko Karadzhov
    Boyko Karadzhov avatar
    56 posts
    Registered:
    05 Dec 2016
    21 Nov 2013
    Link to this post
    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
8 posts, 0 answered