Knowledge Base

Working With Custom Hooks: OnEmptyAssetRecycleFolder

When the Recycle Folder is emptied, all content that it once held is permanently deleted. Use this custom hook to guard yourself from the accidental loss of important assets.


In the context of the Ingeniux CMS platform, assets refer to content items like images, PDF documents, and even sometimes code that are stored in the CMS and published out to a publishing target. Assets differ from page types and components – which are created, edited, and managed directly in the CMS Site Tree – in that they are uploaded, or brought into the CMS, from outside of the platform. Once uploaded, they are stored in the Asset Management System (and usually placed into an Asset Schema) where they can be managed, edited in certain ways, and have metadata assigned to them. 

When a content item is deleted in the Asset Tree it is moved to the Recycle Folder, which is located at the bottom of the Asset Tree. Those items can stay in the Recycle Folder indefinitely until permanently deleted.  

There are two ways to permanently delete content items in the Recycle Folder:  

  • Individually: By right-clicking on the content item and selecting Delete. 
  • All: By right-clicking on the Recycle Folder and selecting Empty Recycle Folder 

The first method triggers the deletion hooks, but not this hook. The method of emptying the Recycle Folder triggers this hook.  

In both methods, items are permanently deleted from the CMS. While their content could be recreated, their IDs cannot be brought back using any method except a full replacement of the CMS from backup. 

Both methods mentioned above use the right-click context menu to delete items from the Recycle Folder. This menu can be managed on a group policy in Administration > System Options > CMS > Asset Tree Context Menu 

How To Trigger the Hook  

This hook may be triggered when a user takes the following action(s): 

  • Right-clicking the Recycle Folder and selecting Empty Recycle Folder. 
  • Triggering a script that calls the EmptyAssetRecycleFolder method in the CSAPI. 

When to Use This Custom Hook  

Due to the permanent nature of removing an item from the Recycle Folder, administrators might want to take advantage of this hook to apply more control around the permanent deletion of content. This hook can be used to check things like who is attempting to perform the action, the status of the item before deletion, or to thoroughly log all the items being permanently removed. 

Considerations 

Periodically emptying the Recycle Folder is a good thing: It helps the CMS run faster and improves performance for many end-user experiences, such as publishing and preview. That said, no one wants to find themselves in a scenario where important content is accidentally permanently deleted.  

Administrators can control the process leading up to emptying the Recycle Folder by using security, permissions, system options, custom hooks, and education. Administrators are immune to security and permission controls. 

If your CMS has a large number of items regularly going into the Recycle Folder, you may want to have an automated script that empties the items from the folder during downtimes to limit impact on end-users. This hook could be used in that case to control the process so it cannot be done during high-traffic times. 

Example 

Store a report of all removed assets 

Emptying the Recycle Folder is the final act of removing items from the CMS. Items can be stored in the folder indefinitely, and you can recover them when you need. Once they are removed from the folder by emptying or by deleting them individually, the items and their data would have to be manually reconstructed, and their IDs are permanently removed and unrecoverable. Because of this finality, you may want to keep a list of items that were removed by emptying the folder in case there were something important found missing later. 

First, define the file that you want to store the report in. You can use an asset in the asset system as your storage for your report. You will want to set which folder you want to store the report file in. 

Get the asset schema for the csv asset type, so you can create the CSV file. Here you can use the StringBuilder method to add lines to the CSV for each file that is removed, adding their name, ID, and schema name. 

Finally, pull those together and create the file. The appended date in the filePath should ensure that you don’t run into duplicates. 

string filePath = "Empty Asset Recycle Folder Report -" + DateTime.Today.ToString("yyyy-MM-dd") + ".csv"; 

var assetfolder = session.Site.AssetFolder("af/21"); 

IAssetSchema csvSchema = session.SchemasManager.AssetSchema(AssetType.Text); 

StringBuilder sb = new StringBuilder(); 

sb.AppendLine("Asset Name, Asset ID, Asset Schema"); 

foreach (var item in recycleFolder.Children(out int childCount)) 

{ 

     sb.AppendLine(item.Name + "," + item.Id + "," + item.SchemaName); 

} 

session.Site.CreateAsset(assetfolder, csvSchema, filePath, false, new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()))); 

Ways to Extend the Functionality Further  

Make sure to check for null objects and have a backup plan. If the asset folder is missing, where should the report be created? 

This could take some time to process if you wait a long time to empty the Recycle Folder and there are many content items inside of it. Consider automated tasks that clean up your CMS on a regular basis and moving this script to one of those. An automated task could be run during CMS downtime for limited impact on performance. 

  • PRODUCT: CMS
  • VERSION: CMS 10
  • RELEASE: 10.x
  • Published: July 3, 2023
  • LAST UPDATED: September 18, 2023
  • Comments: 0

Please login to comment

Comments


There are no comments yet.