Knowledge Base

Working With Custom Hooks: OnAssignAsset

Assigning an asset to a user or group allows them to make changes to the asset. We explore how this custom hook can help provide boundaries around how assets are assigned.


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. 

To edit an asset (e.g., renaming, categorizing, adding or editing metadata, etc.) the asset needs to be checked out and assigned to a user or group. When an asset is assigned to a user or group, they have the ability to edit that asset.  

Let's take a look at how the OnAssignAsset custom hook can be utilized to apply parameters around asset assignment. 

How To Trigger the Hook  

This method is triggered through the following actions: 

  • Right-clicking on an asset, selecting Assign to… from the context menu, and choosing one of the options presented. 
  • Clicking the Assign To button in the toolbar and choosing one of the options presented. 
  • Transitioning an Asset Tree item through workflow, which includes: 
    • Right-clicking an item in the Asset Tree, selecting Add to Workflow, and selecting a workflow. 
    • Right-clicking an item in the Asset Tree, selecting Advance, and following the prompts to finish the advancement. 
    • Clicking the Add to Workflow button in the toolbar. 
    • Clicking the Advance in Workflow button in the toolbar and following the prompts to transition the item. 
  • Syncing a checked-in asset item from the Schema Designer with the Sync pages without checking out box unchecked. 

When to Use This Custom Hook  

Assigning assets doesn’t happen very often since assets are usually uploaded, sent live, and then left alone. You might be interested in taking advantage of that fact and using assignment as a deliberate action for communicating with users.  

Consider a scenario where you have a large number of assets that you want someone to review to determine if they are still needed in the CMS. You could assign the containing folder to the reviewing user and have the CMS assign all items within the folder along with it. Or, because assets can be files with more information inside them, perhaps you upload a CSV with a list of assets that need review and assign that to the user.  

Considerations 

Avoid assigning an Asset Tree content item from within the hooks for assignment to prevent infinite loops. Be especially careful with putting large processes into these hooks, as they are callable through multi-select right-click context and with the potential for the entire collection of assets to be assigned all at once, your code could be processing on a large amount of content items with a single action.  

Example  

Check out related assets 

When an asset is being edited in the CMS, the user has the option to select "save as” to save a related asset. This is often done for image assets, when resizing or cropping, to show that the new image is a rendition of the same subject.  

In this example, consider that those renditions might need to be updated along with each other to stay in sync, so when one is checked out, we want to ensure that all related assets are checked out along with it and assigned to the same user.  

This scenario requires two parts.  

First, you need to create a class outside of the assign hook to work with our related asset list outside the scope of the current item being worked on. 

Outside of the hook:

public class GlobalListHolder  

{  

public static List<IAsset> currentRelatedAssets = new List<IAsset>();  

} 

Once you have that data type to work with, you can move onto the second part: Inside the hook, gather the data for the related items, converting them to a list. 

Ensure that the list in fact exists and that it contains some items before looping through those items and add them to the global list to track them with the transaction. This is essential, as the items in the asset tree will not be assigned until the transaction is complete and you can’t terminate your conditionals, resulting in an infinite loop. 

After you add the item to the list, assign it to the new user.  

Inside of the hook:

List<IAsset> relatedList = session.Site.RelatedAssets(contentItem as IAsset, out int count).ToList(); 

var assigne = contentItem.AssignedUser; 

  

if(relatedList != null && relatedList.Any()) 

{ 

foreach (var item in relatedList) 

{ 

if (!GlobalListHolder.currentAssignments.Contains(item)) 

{ 

GlobalListHolder.currentAssignments.Add(item); 

item.AssignUser(assigne, "assigning related assets"); 

} 

  

} 

}

Ways to Extend the Functionality Further  

You may consider extending this function by notifying the user of the other items that get assigned, otherwise they may be unaware that these additional items were assigned to them. 

A related scenario to consider is you have a large number of assets that have been edited in the CMS and have had new versions of them saved. Consider adding a different way of tracking which items are related to ensure that they are valuable to process together, such as adding them to a custom field on the asset when you link them. 

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

Please login to comment

Comments


There are no comments yet.