Knowledge Base

Working With Custom Hooks: OnMoveAsset

Moving an asset in the Asset Tree can sometimes have unforeseen consequences. Find out how this custom hook can help you track moved assets and alert relevant parties of the change.


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. 

New assets are created into a folder. Folder location and hierarchy within the Asset Tree determine the URL of the asset. When an asset folder moves, the system automatically moves any descendant items along with it. Moving assets changes their path on the live site, though it also creates a redirect from the old path.  

Administrators can control who is allowed to move items in the Asset Tree with a combination of group permissions and security. 

How To Trigger the Hook  

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

  • Left-clicking and dragging an Asset to another location in the Asset Tree. 
  • Right-clicking an Asset and selecting Cut from the context menu, then right-clicking a Folder and selecting Paste. 

When to Use This Custom Hook  

There are many reasons to take advantage of scripts in these hooks, such as updating content that is specific to a new section into which the item was placed, updating or moving related content, or preventing moves to incorrect areas. 

Considerations 

Hopefully, you won’t move assets around too much because when your site was implemented and set up, a good Asset Tree organizational structure and system was put into place.  

But things happen, and sometimes you will have to move assets around. This hook might be used temporarily during a big re-organization of the Asset Management System in the CMS.  

Write code efficiently and be sure to take into account that the hook only triggers for a folder once, not each time for all the contents of the folder or the other folders inside. 

Example  

Notify the creator of the asset when the asset is moved 

In the CMS you have a few ways to determine ownership of a content item, including the creator of the item or the last person to edit the item.  

In this example, we'll send the creator of the asset an email notification whenever the asset is moved.  

First, retrieve the ID of the user who created the asset using the CreationUser property. Then use the value of that property in the User method to get the IUser object representing the creator. 

If the creator is found (if (creator != null)), proceed to send an email notification to the creator's email address.  

To send the email, set up the SMTP server configuration variables : formsSmtpServer, formsSmtpServerPort, formsSmtpEnableSSL, formsSmtpUsername, formsSmtpPassword, and fromEmail. 

Next, create an instance of the SmtpClient class, passing the formsSmtpServer as the SMTP server address. 

If formsSmtpEnableSSL is set to true, then set the EnableSsl property of the SmtpClient to true and provide the username and password for authentication using formsSmtpUsername and formsSmtpPassword. 

Set the Port property of the SmtpClient to formsSmtpServerPort. 

ICreate a new instance of the MailMessage class and set the From address, To address (using the creator's email), Subject, Body, BodyEncoding, and IsBodyHtml properties. 

Finally, use the smtpServer.Send(mail) method to send the email. If an exception occurs while sending the email, catch the exception and log an error message using session.Info. 

string createdUserID = sourcecontentItem.CreationUser; IUser creator = session.UserManager.User(createdUserID); if(creator != null) { string creatorEmail = creator.EmailAddress; if (!string.IsNullOrWhiteSpace(creatorEmail)) { try { var formsSmtpServer = "mx.ingeniuxondemand.com"; var formsSmtpServerPort = 25; var formsSmtpEnableSSL = false; var formsSmtpUsername = ""; var formsSmtpPassword = ""; var fromEmail = "noreply@ingeniux.com"; SmtpClient smtpServer = new SmtpClient(formsSmtpServer); if (formsSmtpEnableSSL) { smtpServer.Credentials = new System.Net.NetworkCredential( formsSmtpUsername, formsSmtpPassword); smtpServer.EnableSsl = true; } smtpServer.Port = formsSmtpServerPort; MailMessage mail = new MailMessage(); mail.From = new MailAddress(fromEmail); mail.To.Add(creatorEmail); mail.Subject = "Asset has Moved"; mail.Body = "<h2>Hello Your Asset" + sourcecontentItem.Id + " - " + sourcecontentItem.Name + "has been Moved</h2>"; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = true; smtpServer.Send(mail); } catch (Exception e) { session.Info("Error sending email" + e); } } } 

Ways to Extend the Functionality Further 

To extend this example it is important to think of your whole CMS environment and how things process in the API. 

When an asset folder is moved, this hook is not triggered individually for each item in the folder. In that case you could add to the method to make a compendium email of all items in the folder that are moved and give a list to each of the individual owners of all items moved. 

When a big migration is done in the CMS, the creator could be a script user or the system itself, depending on how the migration was completed. You could add a user to email for all items that weren’t created by a user that actually logs into the CMS. 

Sending individual emails might get overwhelming in the CMS, instead you could keep a log with the move custom hook over a day or week, depending on how often things move, and send the log to the users later using the automated task framework. 

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

Please login to comment

Comments


There are no comments yet.