Try our conversational search powered by Generative AI!

How to trigger in MVC when a block is deleted

Vote:
 

Hi,

I want to trigger on a block delete and want to maintain some logs when i delete a block in episerver. I am using asp mvc, Please suggest me how to achieve that. When any blocks get deleted it should trigger that a specific block is deleted. Please tell me a way. Thanks in advance.

#144889
Feb 22, 2016 11:29
Vote:
 

Hi

If you want to react to content changes, you should attach to the event of IContentEvents.

You can find some examples here, http://jondjones.com/how-to-hook-into-the-episerver-cms-events-pipeline/.

Regads

Per Gunsarfs

#144899
Feb 22, 2016 12:51
Vote:
 

I went through the blog, I didnt find the event which i should use for deleting a block i.e when in edit mode i remove a block it should immediately fire . I used all the deleting and moving content block but didnt get the result.

#144902
Feb 22, 2016 14:05
Vote:
 

Hi Abhijit,

This is the code I've been using in Alloy, EPiServer CMS 9.7

using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using InitializationModule = EPiServer.Web.InitializationModule;

namespace Alloy.Business
{
    [ModuleDependency(typeof(InitializationModule))]
    public class BlockDeleteHandler : IInitializableModule
    {
        private IContentEvents _contentEvents;

        public void Initialize(InitializationEngine context)
        {
            if (_contentEvents == null)
            {
                _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
            }

            _contentEvents.PublishingContent += ContentEvents_PublishingContent;
            _contentEvents.DeletedContent += ContentEvents_DeletedContent;
            _contentEvents.MovedContent += ContentEvents_MovedContent;
            _contentEvents.MovingContent += ContentEvents_MovingContent;
            _contentEvents.DeletingContent += ContentEvents_DeletingContent;
        }

        private void ContentEvents_PublishingContent(object sender, EPiServer.ContentEventArgs e)
        {
            var block = e.Content as BlockData;
            if (block != null)
            {

            }
        }

        private void ContentEvents_MovingContent(object sender, EPiServer.ContentEventArgs e)
        {
            var block = e.Content as BlockData;
            if (block != null)
            {

            }
        }

        private void ContentEvents_MovedContent(object sender, EPiServer.ContentEventArgs e)
        {
            var block = e.Content as BlockData;
            if (block != null)
            {

            }
        }

        private void ContentEvents_DeletingContent(object sender, EPiServer.DeleteContentEventArgs e)
        {
            var block = e.Content as BlockData;
            if (block != null)
            {

            }
        }

        private void ContentEvents_DeletedContent(object sender, EPiServer.DeleteContentEventArgs e)
        {
            var block = e.Content as BlockData;
            if (block != null)
            {

            }
        }

        public void Uninitialize(InitializationEngine context)
        {
            if (_contentEvents == null)
            {
                _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
            }

            _contentEvents.PublishingContent -= ContentEvents_PublishingContent;
            _contentEvents.MovingContent -= ContentEvents_MovingContent;
            _contentEvents.MovedContent -= ContentEvents_MovedContent;
            _contentEvents.DeletingContent -= ContentEvents_DeletingContent;
            _contentEvents.DeletedContent -= ContentEvents_DeletedContent;
        }
    }
}

You cannot delete block directly, you have to move it to recycle bin first. When you do that, MovingContent and MovedContent events are triggered.
var block = e.Content as BlockData is not null and you know what's been deleted (moved to the recycle bin).

When you empty the recycle bin, DeletingContent and DeletedContent are triggered, but this time var block = e.Content as BlockData is null, and you don't know what have you just deleted.

It looks like a bug to me.

#144910
Feb 22, 2016 15:32
Vote:
 

I guess one could argue if e.Content should be null or not on DeletingContent, but on DeletedContent the content is obviously gone.


What are you trying to achive here Abhijit? There is already an audit log in admin mode where almost everthing is logged.

#144935
Feb 22, 2016 22:34
Vote:
 

Btw, I think there is a list of references in the event args, since you might delete a whole tree of content.

#144936
Feb 22, 2016 22:36
Vote:
 

Actually is not only about maintaining logs, actually user can add blocks as favourite. So , when the block it deleted it should be notified and if the block is favourite it should also get deleted from my Database. So , i need the triggering when the block gets deleted.

#144946
Feb 23, 2016 7:30
Vote:
 

when we try we remove the content neither of the events fire. So , is there a bug in episerver or is there a solution for it. Can you please reply me back.

#144956
Feb 23, 2016 11:36
Vote:
 

Can you share some code? Which version of episerver cms are you using?

#144969
Feb 23, 2016 13:06
Vote:
 

Hi dejan,

the code which you have refered i am using the same. When i remove a block in the edit mode it should trigger me a event . Idle I think it should trigger me ContentEvents_DeletingContent event which is not happening. 

#144973
Edited, Feb 23, 2016 13:49
Vote:
 

 @abhijit pattnaik: When you say "deleting a block", do you mean to detect when a block is removed from a content area on a page (using "Remove" on the Content Area) OR do you mean the action when a user click on "Move to Trash" on a block in the asset pane?

#144976
Feb 23, 2016 14:10
Vote:
 

Check move event and if target is trash can add well :)

#144979
Feb 23, 2016 14:21
Vote:
 

@fredrik: ya i am using remove on the content area . when i move to trash in the asset pane it triggers move event but , i want both the stuffs to be triggered in remove and content area. I am stuck with this for a while now. Can you solution please.

#145043
Feb 24, 2016 6:52
Vote:
 

Store the blocks of a content area in publishing event in context.items. Check again when published event triggers and check what blocks have been removed from content area and log...?

#145047
Feb 24, 2016 7:43
Vote:
 

@abhijit pattnaik: Removing a block from a content area will be much more difficult to detect. The list of referenced blocks is stored in the Content Area property of the page you are editing. When you save that page events will be triggered (save, publish, etc) for the page and you can iterate through all properties (using the Properties-collection) to see if a Content Area property has the flag IsModified set to detect if a content area is changed or not.

To be able to see what is added or removed you need to implement logic that compares each Content Area-propertys list of referenced items with the previously saved version.

Depending on if you want to trigger whenever you save or only when you publish the strategy to do that may differ. 

One solution could be to use ContentVersion class to load currently published version of the page and compare to figure out changes.

#145049
Feb 24, 2016 7:56
Vote:
 

Is there any better solution using any of the events. I saw in removing the block _contentEvents_SavingContent fires, but it more for a page. Can i use event to solve my purpose. Please let me known.

#145082
Feb 24, 2016 11:39
Vote:
 

Hi,

Can you please share your approach with me @Fedrik , may be you share a sample of code of hw achieve. I was just debugging the code i got through the propertycollection and got each property but didnt get the approach of how to achieve it. Thanks a lot for the constant replies, got a lots of ideas of handling lots of other things.

#145113
Feb 25, 2016 10:50
Vote:
 

1. Register event handlers for saving event and saved event for the page. See Dejans example above...

2. In saving event, get the page that is being saved, get the block ids from the content area by ContentArea.Items. Use the contentlink.ID property.

3. Store those ids in a List<int>, store it in memory somewhere, preferably in httpcontext.items collection since you only need it for the request but a short lived cache also works. Now you know the ids of all blocks before the change by editor.

4. In saved event, get a new list of ids like above. Now you know the ids after editor change. Some block ids will be missing. Handle those by logging or whatever you want to do...

#145114
Feb 25, 2016 11:40
Vote:
 

@abhijit pattnaik: I'm sorry but I do not have any code to share with you. I would have to create it from scratch and as I said it is a little bit complicated so it takes more time than I can offer for free. :(

But basically you would have to implement a compare function that is called for each changed property when the PublishingEvent is triggered that takes to ContentArea properties, one from the currently published version (that you also need to Get first) and the other from the item that is being published now. 

I'm not sure why you need to detect when blocks are removed from a Content Area. To me it sounds like a strange solution so if you could elaborate a little more on the original requirement maybe we could help you with other aproaches that are simpler to solve?

#145117
Feb 25, 2016 12:01
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.