Knowledge Base

Working With Custom Hooks: OnDelete

There are many good reasons to enhance this custom hook to meet site requirements - including security and legality. Learn more about how this custom hook can help you avoid accidentally deleting the wrong content.


When a user deletes a content item from the Site Tree, that content item gets moved to the Recycle Folder. When sent to the Recycle Folder, content items are unmarked for publish from all publishing targets and unassigned.

Users can trigger this action manually using the following methods:

  • Right-clicking on a Site Tree content item and selecting delete from the context menu.
  • Right-clicking on a Site Tree content item, selecting cut, right-clicking on the Recycle Folder and selecting paste.
  • Clicking and dragging a Site Tree content item into the Recycle Folder.

Users might use this hook for many reasons, such as preventing certain items form being deleted, notifying content owners that their content items were deleted, tracking extended information about deleted items, etc.

Considerations

When a content item is deleted, it’s children (descendant content items) are also deleted. So, the hook will be called for the content item and its children.

Performance of the code in any scripts should be tested for efficiency.

As always, when working with the page object that is given to you within this hook, remember that you are working with a transactional copy of the item. The transactional item will have properties to give us different information about how it exists in the Site Tree depending on if you look at it in the before hook or the after hook. If you access the Site Tree directly you will still notice the item in the organization because it isn’t moved until after the transaction is complete.

Example

Add to the CSAPI logs information about the deleted item

Since an item that is moved to the Recycle Folder is no longer marked for publish, you may want to record the information about how it was marked for publish before it was deleted in case you want to restore the content item in the future.

You will need to gather all of the publishing targets and see which versions of the content item were marked for those publishing targets. You can use the CSAPI log by calling the Info, Warn, or Error methods to add messages into the log at your desired level. This example uses Warn.

int outVar;

string infoMessage = "";

var pubTargets = session.PublishingManager.Targets(out outVar);

infoMessage += "Deleted item " + page.Id + " marked pub targets and versions: ";

if (!page.MarkedForPublish())

{

     infoMessage += "No marked targets.";

}

foreach(var pubTarget in pubTargets)

{

     if(page.MarkedForPublishOnTarget(pubTarget))

{

          infoMessage += pubTarget.Name + " at version # " + page.MarkedVersionNumber(pubTarget) + ". ";

}

}

session.Warn(infoMessage);

Since the content item is also unassigned when it is deleted, you may enhance this further by adding information about the user or group who had possession of the item before it was deleted.

  • PRODUCT: CMS
  • VERSION: CMS 10
  • RELEASE: 10.x
  • Published: July 22, 2022
  • LAST UPDATED: March 8, 2023
  • Comments: 0

Please login to comment

Comments


There are no comments yet.