Working With Custom Hooks: OnCopyAsset
There are many reasons users may need to copy assets - and much potential for chaos if there is not a system in place to keep things organized. Learn how this custom hook can help site administrators manage copied assets and keep the Asset Tree organized.
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.
Copying and pasting an asset is a pretty rare occurrence. Most users will begin the process of creating a new asset by uploading a new file. There are some reasons that a user might copy and paste an existing asset, and those usually involve assets that are storing files one might edit within the CMS, such as css, xml, html, dita, and other similar code or content type assets.
When a user copies an asset, all aspects of the asset are copied, including the metadata, taxonomy categories, and associated files. The new version of the asset that was copied is created either checked in or out to match the source item.
How To Trigger the Hook
This hook may be triggered when a user takes the following action(s):
- Right-clicking on the content item in the Asset Tree and selecting Copy, then right-clicking on another content item (e.g., a Folder or Page) and selecting Paste or Paste Without Children.
- Highlighting a content item or multiple content items in the Asset Tree, holding CTRL, and dragging the content item(s) to a new location in the Asset Tree.
When to Use This Custom Hook
Administrators may want to use this hook to control aspects of the copied item, such as making sure that the item is checked out and editable, cleaning the metadata, or double checking the taxonomy categorization.
Considerations
Keep in mind that the hook does not trigger on the action of copy being selected from menus, but rather the subsequent paste action. Additionally, this hook is not called by using cut and paste from the right-click context menu – it only applies to copy and paste actions.
As with all hooks that can simultaneously be called by many triggers, you should take care to think about scenarios where a user has inadvertently copied many content items. When a content item with child content items is copied, all of the child content items are also copied. So, when that content item is subsequently pasted, the system automatically wants to paste all the copied child content items as well and the hook will process against all the items that were copied. To avoid this, users will need to select paste without children when they paste the copied content item.
If a user copies a large number of content items, it may take the hook a while to process all of the copies. Keep code as concise as possible and consider moving the functions of the script to an evening cleanup if the traffic flowing through it gets too high.
Example
Stop copy if the asset type is CSHTML
This example gets you into thinking about the extension of an asset and whether you would like to process actions considering the type. Here, the concern is with CSHTML files, which have a very specific purpose in the CMS and could cause trouble if misused. To prevent issues, the script cancels copying the asset.
First, gather your variables, get the extension and set which extension you are concerned about. Then, if the extension of the attempted item matches your concern, throw an exception. Remember that any exception will cancel the process completely. Make sure to give a good error message to the user so they know why their copy failed.
string extension = sourcecontentItem.TechnicalMetadata.Extension;
string excludeExtention = ".cshtml";
if(extension == excludeExtention)
{
throw new Exception("Cant Copy Contact Admin");
}
Ways to Extend the Functionality Further
You may want to limit the types of users who can take this action by adding in parameters that define a specific group, like a developer group, as allowed to perform the action. Consider adding a check for who is attempting the copy.
You may want to use the messaging system to display a message that is more friendly to the user, rather than attempting to communicate through the exception window, which also contains error message content.
There are no comments yet.