November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
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
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.
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.
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.
Btw, I think there is a list of references in the event args, since you might delete a whole tree of content.
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.
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.
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.
@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?
@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.
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...?
@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.
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.
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.
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...
@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?
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.